PIL and jpg -> png conversion

  • Thread starter Raaijmakers, Vincent (IndSys,GE Interlogix)
  • Start date
R

Raaijmakers, Vincent (IndSys,GE Interlogix)

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:

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

and
2)
s = StringIO()
image.save(s,'PNG')
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.
Vincent
 
E

Erik Max Francis

Raaijmakers said:
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.

Presumably PIL in general needs seek ability while reading files. If
that means you can't use StringIO, and you don't want files lying around
you have to clean up, why not use os.tmpfile? It might be spooled to
disk (then again, it might not; it could spool to a RAM disk), but it'll
allow you to do what you need and there won't be any leftover files
lying around afterwards.

--
Erik Max Francis && (e-mail address removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ The only way to get rid of a temptation is to yield to it.
-- Oscar Wilde
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top