new images from strings using PIL

Geoff Gerrietts ggerrietts at yahoo.com
Sun Mar 3 17:01:24 EST 2002


Quoting Max M (maxm at mxm.dk):
> BL wrote:
> 
> >pixels = "1 2 3 4 5 6" //test pixels
> >im = Image.fromstring("I", (2,1), pixels) //create a 3x2 image from pixels
> >These values are obviously wrong.  Why isn't my 3x2 image being created 
> >with the proper pixel intensities?

I can't answer this question exactly, because I'm not familiar with
the "I" image format. However, I can offer some insight into what
"fromstring" and "tostring" expect to do.

The string is straight binary data. Max is right about that. But it's
not stored as a particular file format. The string needs to be
represented more or less like it will be internally stored. (This
isn't entirely true, because PIL does some padding of bytes in its
internals, but it's mostly true.)

RGB and RGBA are what I'm most familiar with, because that's what I
generally work with. For those formats, you're looking for a sequence
of 3 or 4 bytes per pixel, respectively. For instance, your 3x2 image
in all white might look like:

s = "\xFF" * 3 * 6
p = Image.fromstring("RGB", (3,2), s)

Again, I'm not sure how the "I" format works, but my guess is that
you're going to want to pack your integers into a string.

Thanks,
--G.

-- 
Geoff Gerrietts <ggerrietts at yahoo.com>
-rw-rw-rw-:  permissions of the beast




More information about the Python-list mailing list