[Image-SIG] Re: how can I use img.tostring()

Fredrik Lundh fredrik@pythonware.com
Wed, 16 Apr 2003 07:14:07 +0200

"DrTebi" <drtebi@yahoo.com> wrote:

> Following are two examples, the first doesn't work, the second does.
> The working example is fine, but my problem is that I want to actually use
> the thumb data for a shell script (to be exact: I am using the XFS file
> system on a Gentoo Unix system and am trying to write a python script that
> will set a "thumbnail" attribute as binary data to the original image).
> What am I understanding wrong with tostring() ?
> First Example
> -------------
> # this doesn't work
> import Image
> img = Image.open('1882.jpg')
> img.thumbnail((128,128))
> thumb = img.tostring()
> print """Content-type: image/jpeg\r\n"""
> print thumb

tostring returns raw pixel data.

if you want to save a file to an interchange format, save
it to a StringIO buffer:

    file = StringIO.StringIO()
    img.save(file, "JPEG")
    thumb = file.getvalue()