packing greyscale values

J

jimgardener

hi
i am getting value of a pixel of a greyscale image using PIL's
image.getdata and need to pack it as an int as below

ie greyvalues
127 , 56 , 135 , 99 , 102 , 101 , 146 , 112 , 155 , 154 , 112 , 169

should become
-4473925, -8289919, -4144960, -5789785, -5592406, -5658199 ,-3684409,
-5131855, -3289651,-3355444, -5131855, -2763307

(i got these values using java's BufferedImage class..)I would like to
know if there is a way to do the conversion in python.(.i need 'these
'for some calculations)

thanx
jim
 
D

Diez B. Roggisch

jimgardener said:
hi
i am getting value of a pixel of a greyscale image using PIL's
image.getdata and need to pack it as an int as below

ie greyvalues
127 , 56 , 135 , 99 , 102 , 101 , 146 , 112 , 155 , 154 , 112 , 169

should become
-4473925, -8289919, -4144960, -5789785, -5592406, -5658199 ,-3684409,
-5131855, -3289651,-3355444, -5131855, -2763307

(i got these values using java's BufferedImage class..)I would like to
know if there is a way to do the conversion in python.(.i need 'these
'for some calculations)

I have no idea how the bytes from PIL are possibly mapped to the wicked
numbers Java seems to generate (although there might be some systematics to
it). But judging from the fact that

112->-5131855

appears twice, I presume as last resort you could create an explicit mapping
of 0..255 to the values Java generates. To get these, create an image, 256
pixels wide, one high, with grey-values from #000000 to #ffffff. Then build
up the mapping. Not elegant, but works.

Diez
 
P

Peter Otten

jimgardener said:
hi
i am getting value of a pixel of a greyscale image using PIL's
image.getdata and need to pack it as an int as below

ie greyvalues
127 , 56 , 135 , 99 , 102 , 101 , 146 , 112 , 155 , 154 , 112 , 169

should become
-4473925, -8289919, -4144960, -5789785, -5592406, -5658199 ,-3684409,
-5131855, -3289651,-3355444, -5131855, -2763307

(i got these values using java's BufferedImage class..)I would like to
know if there is a way to do the conversion in python.(.i need 'these
'for some calculations)

Judging from
187

-4273925 indeed seems to denote a gray value, but the lighter 187 rather
than 127. Are you sure the lists you give belong to the same image?

Peter
 
J

jimgardener

Are you sure the lists you give belong to the same image?

the greyvalues list was printed out by python script on a 3X4
sampleimage i made to check this

im=Image.open("mypicgrey.jpg")
pixels=im.getdata()
for pix in pixels:
print pix

whereas the integers in the second list were from java code

FileInputStream fIn = new FileInputStream("mypicgrey.jpg");
JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);
BufferedImage image = jpeg_decode.decodeAsBufferedImage();
int width = image.getWidth();
int height = image.getHeight();
int[] rgbdata = new int[width * height];
image.getRGB(0,0,width,height,rgbdata,0,width);

'rgbdata' now has the second list of integer values

if it was an rgb image with r,g,b values i could pack it as
return unpack("l", pack("BBBB", b, g, r, alpha))[0]
(where alpha=255)

but i don't know how to do this for a greyscale image
jim
 
D

Dennis Lee Bieber

the greyvalues list was printed out by python script on a 3X4
sampleimage i made to check this

im=Image.open("mypicgrey.jpg")
pixels=im.getdata()
for pix in pixels:
print pix
Might I suggest you print using hex formatting, rather than decimal
integer? It might reveal any patterns...
whereas the integers in the second list were from java code

FileInputStream fIn = new FileInputStream("mypicgrey.jpg");
JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);
BufferedImage image = jpeg_decode.decodeAsBufferedImage();
int width = image.getWidth();
int height = image.getHeight();
int[] rgbdata = new int[width * height];
image.getRGB(0,0,width,height,rgbdata,0,width);

'rgbdata' now has the second list of integer values
.... print "%2.2X" % s,
....
7F 38 87 63 66 65 92 70 9B 9A 70 A9.... print "%8.8X" % ((2**32) + s),
....
FFBBBBBB FF818181 FFC0C0C0 FFA7A7A7 FFAAAAAA FFA9A9A9 FFC7C7C7 FFB1B1B1
FFCDCDCD FFCCCCCC FFB1B1B1 FFD5D5D5

Unfortunately, the values don't seem to bear any correlation --
other than appearing to be alpha, RGB
.... print "%2.2X" % ((0x000000FF & rgb) - g),
....
3C 49 39 44 44 44 35 41 32 32 41 2C--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Machin

Judging from


187

-4273925 indeed seems to denote a gray value, but the lighter 187 rather
than 127. Are you sure the lists you give belong to the same image?

Peter

Let Y = Java-derived numbers (187, etc)
and X = PIL-derived numbers (127, etc)
Y approx = 0.75 * X + 90
What the underlying cause of this relationship is, I have not a clue.

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top