Connecting Tech Pros Worldwide Forums | Help | Site Map

PIL: how to return a StringIO of an Image

Christoph
Guest
 
Posts: n/a
#1: Jul 18 '05
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

Fredrik Lundh
Guest
 
Posts: n/a
#2: Jul 18 '05

re: PIL: how to return a StringIO of an Image


"Christoph" wrote:
[color=blue]
> image.save('test', 'JPEG')
> image=StringIO(image.????()) # what's the right method?
> return image
>
> Any hints? Thanx![/color]

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

</F>




Christoph
Guest
 
Posts: n/a
#3: Jul 18 '05

re: PIL: how to return a StringIO of an Image


Fredrik Lundh schrieb:
[color=blue][color=green]
>> image.save('test', 'JPEG')
>> image=StringIO(image.????()) # what's the right method?
>> return image
>>
>>Any hints? Thanx![/color]
>
>
> file = StringIO()
> image.save(file, "JPEG")
> return file.getvalue()
>[/color]
That's what I wanted. Perfect!
Thanx a lot!
Christoph

Bengt Richter
Guest
 
Posts: n/a
#4: Jul 18 '05

re: PIL: how to return a StringIO of an Image


On Fri, 14 Nov 2003 16:45:39 +0100, "Fredrik Lundh" <fredrik@pythonware.com> wrote:
[color=blue]
>"Christoph" wrote:
>[color=green]
>> image.save('test', 'JPEG')
>> image=StringIO(image.????()) # what's the right method?
>> return image
>>
>> Any hints? Thanx![/color]
>
> file = StringIO()
> image.save(file, "JPEG")
> return file.getvalue()
>[/color]
You are recommending shadowing 'file' ?!

Regards,
Bengt Richter
JCM
Guest
 
Posts: n/a
#5: Jul 18 '05

re: PIL: how to return a StringIO of an Image


Bengt Richter <bokr@oz.net> wrote:[color=blue]
> On Fri, 14 Nov 2003 16:45:39 +0100, "Fredrik Lundh" <fredrik@pythonware.com> wrote:[/color]
[color=blue][color=green]
>>"Christoph" wrote:
>>[color=darkred]
>>> image.save('test', 'JPEG')
>>> image=StringIO(image.????()) # what's the right method?
>>> return image
>>>
>>> Any hints? Thanx![/color]
>>
>> file = StringIO()
>> image.save(file, "JPEG")
>> return file.getvalue()
>>[/color]
> You are recommending shadowing 'file' ?![/color]

Why not? Python is a block-structured language. New builtins can
arrive in any release.
Bengt Richter
Guest
 
Posts: n/a
#6: Jul 18 '05

re: PIL: how to return a StringIO of an Image


On Fri, 14 Nov 2003 20:38:43 +0000 (UTC), JCM <joshway_without_spam@myway.com> wrote:
[color=blue]
>Bengt Richter <bokr@oz.net> wrote:[color=green]
>> On Fri, 14 Nov 2003 16:45:39 +0100, "Fredrik Lundh" <fredrik@pythonware.com> wrote:[/color]
>[color=green][color=darkred]
>>>"Christoph" wrote:
>>>
>>>> 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()
>>>[/color]
>> You are recommending shadowing 'file' ?![/color]
>
>Why not? Python is a block-structured language. New builtins can
>arrive in any release.[/color]
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
Closed Thread