title: How to send embedded image created using PIL/pillow as email (Python 3)
body: <p>I am creating image that I would like to embed in the e-mail. I cannot figure out how to create image as binary and pass into MIMEImage. Below is the code I have and I have error when I try to read image object - the error is "AttributeError: 'NoneType' object has no attribute 'read'".</p>
  
  <pre><code>image=Image.new("RGBA",(300,400),(255,255,255))
  image_base=ImageDraw.Draw(image)
  emailed_password_pic=image_base.text((150,200),emailed_password,(0,0,0))
  imgObj=emailed_password_pic.read()
  msg=MIMEMultipart()
  html="""&lt;p&gt;Please finish registration &lt;br/&gt;&lt;img src="cid:image.jpg"&gt;&lt;/p&gt;"""
  img_file='image.jpg'
  msgText = MIMEText(html,'html')
  msgImg=MIMEImage(imgObj)
  msgImg.add_header('Content-ID',img_file)
  msg.attach(msgImg)
  msg.attach(msgText)
  </code></pre>
  
  <p>If you look at line 4 - I am trying to read image so that I can pass it into MIMEImage. Apparently, image needs to be read as binary. However, I don't know how to convert it to binary so that .read() can process it.</p>
  
  <p><strong>FOLLOW-UP</strong>
  I edited code per suggestions from jsbueno - thank you very much!!!:</p>
  
  <pre><code>  emailed_password=os.urandom(16)
    image=Image.new("RGBA",(300,400),(255,255,255))
    image_base=ImageDraw.Draw(image)
    emailed_password_pic=image_base.text((150,200),emailed_password,(0,0,0))
    stream_bytes=BytesIO()
    image.save(stream_bytes,format='png')
    stream_bytes.seek(0)
    #in_memory_file=stream_bytes.getvalue()
    #imgObj=in_memory_file.read()
    imgObj=stream_bytes.read()
    msg=MIMEMultipart()
    sender='xxx@abc.com'
    receiver='jjjj@gmail.com'
    subject_header='Please use code provided in this e-mail to confirm your subscription.'
    msg["To"]=receiver
    msg["From"]=sender
    msg["Subject"]=subject_header
    html="""&lt;p&gt;Please finish registration by loging into your account and typing in code from this e-mail.&lt;br/&gt;&lt;img src="cid:image.png"&gt;&lt;/p&gt;"""
    img_file='image.png'
    msgText=MIMEText(html,'html')
    msgImg=MIMEImage(imgObj) #Is mistake here?
    msgImg.add_header('Content-ID',img_file)
    msg.attach(msgImg)
    msg.attach(msgText)
    smtpObj=smtplib.SMTP('smtp.mandrillapp.com', 587)
    smtpObj.login(userName,userPassword)
    smtpObj.sendmail(sender,receiver,msg.as_string())
  </code></pre>
  
  <p>I am not getting errors now but e-mail does not have image in it. I am confused about the way image gets attached and related to in html/email part. Any help is appreciated!</p>
  
  <p><strong>UPDATE:
  This code actually works - I just had minor typo in the code on my PC.</strong></p>