[Tutor] problem in converting pixel data to image file

Lie Ryan lie.1296 at gmail.com
Mon Aug 18 12:41:00 CEST 2008


> Message: 3
> Date: Mon, 18 Aug 2008 14:30:08 +0530
> From: "Ashish Sethi" <ashishbitsgoa at gmail.com>
> Subject: [Tutor] problem in converting pixel data to image file
> To: tutor at python.org
> Message-ID:
>         <edb5c69c0808180200g62b34859j7441d4587d82276b at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
> 
> Hi all,
> I have a problem in converting the pixel data (read from a string and
> written to a file using fromstring command in PIL ).
> The file handle of this file is called buffer. Now, when I tried to
> open the
> file as an image file but it didnt work.

What is the format of the image you want to open? PIL doesn't support
all image format in the world (of course), but the most common ones are
supported. fromstring, by default, assumes that the string you're
passing it is in raw format (i.e. the definition of raw format is
explained PIL's documentation)

> Then I read the documentation of PIL and found this written about
> fromstring
> function
> "Note that this function decodes pixel data, not entire images. If you
> have an entire image in a string, wrap it in a *StringIO *object, and
> use
> *open *to load it."

StringIO is simply a wrapper around string to make it have a file-like
interface (since Python uses Duck Typing principle, if an object has the
same members a file-object have, like read(), write(), readlines(), etc
python should not differentiate them)

> so i wrote the following code....
> 
> file = StringIO.StringIO(buffer)
> img = Image.open(file)
> img.save(file, 'JPEG')
> 
> *Error:*
> img = Image.open(file)
>   File "/home/rahhal/python/lib/python2.4/site-packages/PIL/Image.py",
> line
> 1745, in open
>     raise IOError("cannot identify image file")
> IOError: cannot identify image file

PIL is it cannot identify what format the original image is in, either
you pass explicitly what format the original file is in, or (at the
worst case) you create (or search whether one is available on the
internet) your own decoder.

PS: List of image format supported by PIL (you can also find it in PIL's
documentation):
BMP
BUFR (identify only)
CUR (read only)
DCX (read only)
EPS (write-only)
FITS (identify only)
FLI, FLC (read only)
FPX (read only)
GBR (read only)
GD (read only)
GIF
GRIB (identify only)
HDF5 (identify only)
ICO (read only)
IM
IMT (read only)
IPTC/NAA (read only)
JPEG
MCIDAS (read only)
MIC (read only)
MPEG (identify only)
MSP
PALM (write only)
PCD (read only)
PCX
PDF (write only)
PIXAR (read only)
PNG
PPM
PSD (read only)
SGI (read only)
SPIDER
TGA (read only)
TIFF
WAL (read only)
WMF (identify only)
XBM
XPM (read only)
XV Thumbnails 

> Can any one please help in solving my problem??
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> <http://mail.python.org/pipermail/tutor/attachments/20080818/e7a1c2bd/attachment-0001.htm>
> 
> ------------------------------



More information about the Tutor mailing list