[Image-SIG] going between numpy array and PIL.Image not behaving as expected -- how to report a bug ?
Jim Vickroy
Jim.Vickroy at noaa.gov
Fri Nov 21 20:51:12 CET 2008
Ned Batchelder wrote:
> Yes, it definitely looks like a bug in fromarray. typestr is '<i4' in
> my environment, and the check against typestr[1:] seems to recognize
> this. The line
>
> typestr = typestr[:2]
>
> should change, perhaps to:
>
> typestr = typestr[-2:]
>
> --Ned.
> http://nedbatchelder.com
OK, I have been wandering around http://www.pythonware.com/ for a while
and have somehow missed the URL for bug reporting. Could someone point
me in the right direction? Sorry if I have missed something obvious.
P.S. (Ned)
Thanks for coverage.py; I was a little slow to make the association!
>
> Jim Vickroy wrote:
>> Ned Batchelder wrote:
>>> 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.
>> Thanks for your clear and detailed reply and sorry for the vagueness
>> of my posting; you did correctly read my mind!
>>> 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.
>> Thanks, this does indeed work and your explanation made me wonder why
>> specifying dtype=int (as in my posted script) did not work.
>>
>> Here is the signature of PIL.Image.fromarray in my installation (v
>> 1.1.6):
>>
>> * fromarray(obj, mode=None)
>>
>> so when mode is not specified, the procedure determines it from the
>> attributes of "obj" as follows:
>> if mode is None:
>> typestr = arr['typestr']
>> if not (typestr[0] == '|' or typestr[0] == _ENDIAN or
>> typestr[1:] not in ['u1', 'b1', 'i4', 'f4']):
>> raise TypeError("cannot handle data-type")
>> typestr = typestr[:2] ##### why isn't this: typestr =
>> typestr[1:] or typestr = typestr[1:3] ? ##################
>> if typestr == 'i4':
>> mode = 'I'
>> elif typestr == 'f4':
>> mode = 'F'
>> elif typestr == 'b1':
>> mode = '1'
>> elif ndim == 2:
>> mode = 'L'
>> elif ndim == 3:
>> mode = 'RGB'
>> elif ndim == 4:
>> mode = 'RGBA'
>> else:
>> raise TypeError("Do not understand data.")
>>
>> I am relatively inexperienced with both PIL and numpy, but the
>> statement:
>>
>> * typestr = typestr[:2]
>>
>> seems to be incorrect; after it is executed, I do not see how typestr
>> can ever be any of ('i4' , 'f4', 'b1').
>>
>> If I make the indicated change to
>>
>> * typestr = typestr[1:]
>>
>> then "mode" is correctly inferred from "obj" and I do not have to
>> explicitly specify it when applying the fromarray() procedure.
>>
>> Is this a logic error in fromarray()?
>>>
>>
>>>
>>> --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
>>>
>>
>
> --
> Ned Batchelder, http://nedbatchelder.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20081121/145acfd2/attachment-0001.htm>
More information about the Image-SIG
mailing list