PIL: how to return a StringIO of an Image

C

Christoph

Hi,

I have a script I pass (from Zope) an image file to, to do some PIL image stuff
with it. I want a StringIO instance representing the altered image file to be
returned. What is the method to get the image data (header and pixel data)? I
tried Image.tostring(), but that only returns the pixle data. But to be able to
use the returned data in Zope as an image file (manage_addImage, in case someone
cares), I need the complete representation of the. file.

Here’s some code:

def modify_image(self, my_image):
from PIL import Image
from StringIO import StringIO

image=Image.open(my_image)
. # do some PIL
.
.
image.save('test', 'JPEG')
image=StringIO(image.????()) # what's the right method?
return image

Any hints? Thanx!
Regards
Christoph
 
F

Fredrik Lundh

Christoph said:
image.save('test', 'JPEG')
image=StringIO(image.????()) # what's the right method?
return image

Any hints? Thanx!

file = StringIO()
image.save(file, "JPEG")
return file.getvalue()

</F>
 
J

JCM

Bengt Richter said:
file = StringIO()
image.save(file, "JPEG")
return file.getvalue()
You are recommending shadowing 'file' ?![/QUOTE]

Why not? Python is a block-structured language. New builtins can
arrive in any release.
 
B

Bengt Richter

Why not? Python is a block-structured language. New builtins can
arrive in any release.
I'd say a reason is to avoid the time you will do it by habit and get a surprise.
But yes, there's no technical reason you can't, if you don't want to use the original
binding in that scope.

Regards,
Bengt Richter
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top