PIL: how to return a StringIO of an Image

Discussion in 'Python' started by Christoph, Nov 14, 2003.

  1. Christoph

    Christoph Guest

    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
     
    Christoph, Nov 14, 2003
    #1
    1. Advertising

  2. Re: how to return a StringIO of an Image

    "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()

    </F>
     
    Fredrik Lundh, Nov 14, 2003
    #2
    1. Advertising

  3. Christoph

    Christoph Guest

    Re: how to return a StringIO of an Image

    Fredrik Lundh schrieb:

    >> 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()
    >

    That's what I wanted. Perfect!
    Thanx a lot!
    Christoph
     
    Christoph, Nov 14, 2003
    #3
  4. Re: how to return a StringIO of an Image

    On Fri, 14 Nov 2003 16:45:39 +0100, "Fredrik Lundh" <> wrote:

    >"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()
    >

    You are recommending shadowing 'file' ?!

    Regards,
    Bengt Richter
     
    Bengt Richter, Nov 14, 2003
    #4
  5. Christoph

    JCM Guest

    Re: how to return a StringIO of an Image

    Bengt Richter <> wrote:
    > On Fri, 14 Nov 2003 16:45:39 +0100, "Fredrik Lundh" <> wrote:


    >>"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()
    >>

    > You are recommending shadowing 'file' ?!


    Why not? Python is a block-structured language. New builtins can
    arrive in any release.
     
    JCM, Nov 14, 2003
    #5
  6. Re: how to return a StringIO of an Image

    On Fri, 14 Nov 2003 20:38:43 +0000 (UTC), JCM <> wrote:

    >Bengt Richter <> wrote:
    >> On Fri, 14 Nov 2003 16:45:39 +0100, "Fredrik Lundh" <> wrote:

    >
    >>>"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()
    >>>

    >> You are recommending shadowing 'file' ?!

    >
    >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
     
    Bengt Richter, Nov 14, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    156
    Skip Montanaro
    May 30, 2013
  2. Cameron Simpson
    Replies:
    0
    Views:
    102
    Cameron Simpson
    May 31, 2013
  3. Göktuğ Kayaalp
    Replies:
    0
    Views:
    116
    Göktuğ Kayaalp
    May 31, 2013
  4. Skip Montanaro
    Replies:
    0
    Views:
    114
    Skip Montanaro
    May 31, 2013
  5. Serhiy Storchaka
    Replies:
    0
    Views:
    85
    Serhiy Storchaka
    May 31, 2013
Loading...

Share This Page