[Image-SIG] Fwd: problem in converting pixel data to image file

Fredrik Lundh fredrik at pythonware.com
Mon Aug 18 19:22:58 CEST 2008

Ashish Sethi wrote:

> 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.
> 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."
> 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
> Can any one please help in solving my problem??

What's "pixel data" here?  Image.open expects the file to contain an 
image in a known image interchange format (e.g. JPEG, PNG, etc).  If you 
have raw data, you might be able to use frombuffer or fromstring.


More information about the Image-SIG mailing list