[Image-SIG] going between numpy array and PIL.Image not behaving as expected
Ned Batchelder
ned at nedbatchelder.com
Fri Nov 21 13:04:38 CET 2008
I'm not sure exactly what you are trying to do here, but the issue has
to do with the mapping of numpy array elements into pixels. Your code
uses 32-bit ints, and fromarray defaults to "L" mode, which is 8-bit
grayscale pixels. fromarray uses the shape of the array to create the
shape of the image, but then just reads bytes until the image has all
the data it needs. In your case, it only needs to read 3 32-bit ints to
get enough bytes to fill the 3x4 "L" mode image. In the first three
ints, the min byte is zero and the max byte is 2, which your image
extrema verifies.
If you change your code to use this:
source = numpy.arange(0,12,dtype=numpy.int8)
then everything will match up: your array has byte elements, and your
image will have byte pixels.
--Ned.
Jim Vickroy wrote:
> Hello all,
>
> I am having no success getting numpy and PIL to behave as expected
> when starting with a numpy array (see the attached script).
>
> Here is the output on my computer:
>
> <output>
> Python version: 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310
> 32 bit (Intel)]
> numpy version: 1.2.1
> PIL version: 1.1.6
> numpy source array:
> [[ 0 1 2 3]
> [ 4 5 6 7]
> [ 8 9 10 11]]
> numpy source array shape: (3, 4)
> PIL image size: (4, 3)
> Traceback (most recent call last):
> File "C:\Documents and Settings\jim.vickroy\My
> Documents\Projects\GOES\SXI\__trials__\numpy-PIL.py", line 30, in
> <module>
> ''' % (extrema, image.getextrema())
> AssertionError:
> numpy image extrema (minimum,maximum): (0, 11)
> PIL image extrema (minimum,maximum): (0, 2)
> </output>
>
>
> I would appreciate pointers on what I'm doing incorrectly.
>
> Thanks,
> -- jv
> ------------------------------------------------------------------------
>
> _______________________________________________
> Image-SIG maillist - Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig
>
--
Ned Batchelder, http://nedbatchelder.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20081121/b544c679/attachment-0001.htm>
More information about the Image-SIG
mailing list