Python: Get string value of a PIL Image without writing to a temporary file

It's often useful to fetch the raw image data without having to save it to a file first (eg. as serving images by HTTP or sending email attachments)

To do that, we make use of the class StringIO.

import StringIO

# Save the file to a temporary file because that's how email attachments work
output = StringIO.StringIO(), format = "PNG")
email.attach(filename = "whatever.png", mimetype = "image/png", content = output.getvalue())

And there you have it!



Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog