<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Ned Batchelder wrote:
<blockquote cite="mid:4926FBC3.9080302@nedbatchelder.com" type="cite">
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
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<br>
<br>
typestr = typestr[:2]<br>
<br>
should change, perhaps to:<br>
<br>
typestr = typestr[-2:]<br>
<br>
--Ned.<br>
<a moz-do-not-send="true" class="moz-txt-link-freetext"
href="http://nedbatchelder.com">http://nedbatchelder.com</a><br>
</blockquote>
OK, I have been wandering around <a class="moz-txt-link-freetext" href="http://www.pythonware.com/">http://www.pythonware.com/</a> 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.<br>
<br>
P.S. (Ned)<br>
Thanks for coverage.py; I was a little slow to make the association!<br>
<blockquote cite="mid:4926FBC3.9080302@nedbatchelder.com" type="cite"><br>
Jim Vickroy wrote:
<blockquote cite="mid:4926D79D.7010609@noaa.gov" type="cite">
<meta content="text/html;charset=ISO-8859-1"
http-equiv="Content-Type">
Ned Batchelder wrote:
<blockquote cite="mid:4926A3D6.7070300@nedbatchelder.com"
type="cite">
<meta content="text/html;charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>
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. </blockquote>
Thanks for your clear and detailed reply and sorry for the vagueness of
my posting; you did correctly read my mind!<br>
<blockquote cite="mid:4926A3D6.7070300@nedbatchelder.com"
type="cite">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.<br>
<br>
If you change your code to use this:<br>
<br>
source = numpy.arange(0,12,dtype=numpy.int8)<br>
<br>
then everything will match up: your array has byte elements, and your
image will have byte pixels.<br>
</blockquote>
Thanks, this does indeed work and your explanation made me wonder why
specifying dtype=int (as in my posted script) did not work. <br>
<br>
Here is the signature of PIL.Image.fromarray in my installation (v
1.1.6):<br>
<ul>
<li>fromarray(obj, mode=None)</li>
</ul>
so when mode is not specified, the procedure determines it from the
attributes of "obj" as follows:<br>
if mode is None:<br>
typestr = arr['typestr']<br>
if not (typestr[0] == '|' or typestr[0] == _ENDIAN or<br>
typestr[1:] not in ['u1', 'b1', 'i4', 'f4']):<br>
raise TypeError("cannot handle data-type")<br>
typestr = typestr[:2] ##### why isn't this: typestr =
typestr[1:] or typestr = typestr[1:3] ? ##################<br>
if typestr == 'i4':<br>
mode = 'I'<br>
elif typestr == 'f4':<br>
mode = 'F'<br>
elif typestr == 'b1':<br>
mode = '1'<br>
elif ndim == 2:<br>
mode = 'L'<br>
elif ndim == 3:<br>
mode = 'RGB'<br>
elif ndim == 4:<br>
mode = 'RGBA'<br>
else:<br>
raise TypeError("Do not understand data.")<br>
<br>
I am relatively inexperienced with both PIL and numpy, but the
statement:<br>
<ul>
<li>typestr = typestr[:2]</li>
</ul>
seems to be incorrect; after it is executed, I do not see how typestr
can ever be any of ('i4' , 'f4', 'b1').<br>
<br>
If I make the indicated change to<br>
<ul>
<li>typestr = typestr[1:]</li>
</ul>
then "mode" is correctly inferred from "obj" and I do not have to
explicitly specify it when applying the fromarray() procedure.<br>
<br>
Is this a logic error in fromarray()?<br>
<blockquote cite="mid:4926A3D6.7070300@nedbatchelder.com"
type="cite"> <br>
</blockquote>
<br>
<blockquote cite="mid:4926A3D6.7070300@nedbatchelder.com"
type="cite"><br>
--Ned.<br>
</blockquote>
<br>
<blockquote cite="mid:4926A3D6.7070300@nedbatchelder.com"
type="cite"><br>
Jim Vickroy wrote:
<blockquote cite="mid:4925E193.8070500@noaa.gov" type="cite">Hello
all, <br>
<br>
I am having no success getting numpy and PIL to behave as expected when
starting with a numpy array (see the attached script). <br>
<br>
Here is the output on my computer: <br>
<br>
<output> <br>
Python version: 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310
32 bit (Intel)] <br>
numpy version: 1.2.1 <br>
PIL version: 1.1.6 <br>
numpy source array: <br>
[[ 0 1 2 3] <br>
[ 4 5 6 7] <br>
[ 8 9 10 11]] <br>
numpy source array shape: (3, 4) <br>
PIL image size: (4, 3) <br>
Traceback (most recent call last): <br>
File "C:\Documents and Settings\jim.vickroy\My
Documents\Projects\GOES\SXI\__trials__\numpy-PIL.py", line 30, in
<module> <br>
''' % (extrema, image.getextrema()) <br>
AssertionError: <br>
numpy image extrema (minimum,maximum): (0, 11) <br>
PIL image extrema (minimum,maximum): (0, 2) <br>
</output> <br>
<br>
<br>
I would appreciate pointers on what I'm doing incorrectly. <br>
<br>
Thanks, <br>
-- jv <br>
<pre wrap=""><hr size="4" width="90%">
_______________________________________________
Image-SIG maillist - <a moz-do-not-send="true"
class="moz-txt-link-abbreviated" href="mailto:Image-SIG@python.org">Image-SIG@python.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext"
href="http://mail.python.org/mailman/listinfo/image-sig">http://mail.python.org/mailman/listinfo/image-sig</a>
</pre>
</blockquote>
<br>
<pre class="moz-signature" cols="72">--
Ned Batchelder, <a moz-do-not-send="true" class="moz-txt-link-freetext"
href="http://nedbatchelder.com">http://nedbatchelder.com</a>
</pre>
</blockquote>
<br>
</blockquote>
<br>
<pre class="moz-signature" cols="72">--
Ned Batchelder, <a moz-do-not-send="true" class="moz-txt-link-freetext"
href="http://nedbatchelder.com">http://nedbatchelder.com</a>
</pre>
</blockquote>
<br>
</body>
</html>