PIL and jpg -> png conversion

Raaijmakers, Vincent (IndSys, Raaijmakers, Vincent (IndSys,
Mon Nov 17 22:49:10 CET 2003

In my application I would like to convert a JPG to a PNG format.
Easy, if you save the output to a file.
image.save('file.png', 'PNG')

Now, this is not what I want. In my application I would like to return
the converted PNG file.png data.

2 way how I tried this:

image.save('file.png', 'PNG')
return open('file.png', 'r').read()  #Eeeks

s = StringIO()
return s.getvalue()

Well, because in the first option, I need to read and write to and from disk is not an elegant programming option, I prefer to do the latter. Unfortunately, this seems only to work for GIF and JPEG, but not for PNG.
As a test I try to reconvert the file output to an image again but the image.verify() throws an fp.seek.tile error. This only happens with PNG files.

Has someone good alternative for me and perhaps an answer why this fails? Because of the use of StringIO? Do I need a Stream object. If so, how to use it.

Please help.


More information about the Python-list mailing list