[Image-SIG] Re: tostring

Douglas Bagnall douglas at paradise.net.nz
Sun Apr 18 17:48:49 EDT 2004


hi,

Fredrik Lundh wrote:
>
> if you want to save an image to a string, use the StringIO module:
>
>     import StringIO
>     ...
>     out = StringIO.StringIO()
>     im.save(out)
>     data = out.getvalue()


I've found that this works

    data = im.tostring("jpeg", ('RGB',))

and is a bit quicker than the stringIO method.

Writing directly to sys.stdout is simpler, but beware in a web context -
  some browsers like the content-length header to be set (ie,
print "Content-length: %s" % len(data)).

douglas






More information about the Image-SIG mailing list