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.
1.
import
StringIO
2.
3.
# Save the file to a temporary file because that's how email attachments work
4.
output
=
StringIO.StringIO()
5.
pil_image.save(output, format
=
"PNG"
)
6.
email.attach(filename
=
"whatever.png"
, mimetype
=
"image/png"
, content
=
output.getvalue())
7.
output.close()
And there you have it!