I would like to create an image using PIL and be able to send it by email without saving it to disk.
This is what works, but includes saving to disk:
from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart msg = MIMEMultipart() im = Image.new("RGB", (200, 200)) with open("tempimg.jpg", "w") as f: im.save(f, "JPEG") with open("tempimg.jpg", 'rb') as f: img = MIMEImage(f.read()) msg.attach(img)
Now I would like to do something like:
import StringIO tempimg = StringIO.StringIO() tempimg.write(im.tostring()) img = MIMEImage(tempimage.getvalue(), "JPG") msg.attach(img)
which does not work. I found some discussion in Spanish , it looks like it solves the same issue without a solution other than a pointer to StringIO.
source share