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()
pil_image.save(output, format = "PNG")
email.attach(filename = "whatever.png", mimetype = "image/png", content = output.getvalue())
output.close()

And there you have it!

425770_391213704223668_100000051259380_1581660_222260695_n

Source

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