[Image-SIG] Inter-converting images and Numeric arrays

Fredrik Lundh fredrik@pythonware.com
Sat, 4 Jul 1998 18:24:47 +0100

>1:  Shouldn't PIL support conversion directly to/from arrays?  Using
>the intermediate string works, but seems like a hack...

Recent versions provide getdata and putdata methods (getdata is
even documented):

    data = im1.getdata()
    im2 = Image.new(im1.mode, im2.size)

The sequence object returned by getdata is a bit fragile, at
least under 1.4.  If you get problems, explicitly convert it to
a list or tuple:

    data = list(im1.getdata())

>2:  Seems that if I start with an Array, make an Image, then convert
>back, the y-axis is inverted.  Is this a feature or a bug?

To be compatible with old versions of PIL, the fromstring/tostring
methods by default assumes that the data has the bottom-most line
first, and RGB data has an extra pad byte after each pixel.  To get
other behaviour, you need to specify codec arguments.

However, the recent fromstring *function* doesn't provide the same
default behaviour -- instead, it defaults to more reasonable top-most
line first, RGB data without padding settings.  The problem is that if
you pass the result from tostring directly to the fromstring *function*,
the image is turned upside down.  Again, you can modify this behaviour
using codec arguments.  See:



We're about to release a new version of PIL; I'm tempted to change
the method's default settings so that it matches the fromstring function.
If this would break a huge amount of code for anyone on this list, please
speak up!

Cheers /F