[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

packing greyscale values

jimgardener

2/11/2008 7:19:00 AM

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
6 Answers

Diez B. Roggisch

2/11/2008 9:41:00 AM

0

jimgardener wrote:

> 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

Bearophile

2/11/2008 10:08:00 AM

0

Diez B. Roggisch:
> Then build up the mapping. Not elegant, but works.

For the OP: note that a list suffices, no dict needed.

Bye,
bearophile

Peter Otten

2/11/2008 10:11:00 AM

0

jimgardener wrote:

> 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

>>> hex(0x100000000 - 4473925)
'0xffbbbbbb'
>>> 0xbb
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

jimgardener

2/11/2008 2:45:00 PM

0

>>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

Dennis Lee Bieber

2/11/2008 6:10:00 PM

0

On Mon, 11 Feb 2008 06:45:27 -0800 (PST), jimgardener
<jimgardener@gmail.com> declaimed the following in comp.lang.python:

> 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
>

>>> for s in g:
.... print "%2.2X" % s,
....
7F 38 87 63 66 65 92 70 9B 9A 70 A9
>>> for s in rgb:
.... 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

>>> for s in range(len(rgb)):
.... print "%2.2X" % ((0x000000FF & rgb[s]) - g[s]),
....
3C 49 39 44 44 44 35 41 32 32 41 2C
>>>
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

John Machin

2/11/2008 10:34:00 PM

0

On Feb 11, 9:11 pm, Peter Otten <__pete...@web.de> wrote:
> jimgardener wrote:
> > 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
>
> >>> hex(0x100000000 - 4473925)
> '0xffbbbbbb'
> >>> 0xbb
>
> 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