title: How to convert a string to an image?
body: <p>I started to learn python a week ago and want to write a small program that converts a email to a image (.png) so that it can be shared on forums without risking to get lots of spam mails.</p>
  <p>It seems like the python standard library doesn't contain a module that can do that but I've found out that there's a <code>PIL</code> module for it (<code>PIL.ImageDraw</code>).</p>
  <p>My problem is that I can't seem to get it working.</p>
  <p>So basically my questions are:</p>
  <ol>
  <li>How to draw a text onto a image.</li>
  <li>How to create a blank (white) image</li>
  <li>Is there a way to do this without actually creating a file so that I can show it in a GUI before saving it?</li>
  </ol>
  <p>Current Code:</p>
  <pre><code>import Image
  import ImageDraw
  import ImageFont
  
  def getSize(txt, font):
      testImg = Image.new('RGB', (1, 1))
      testDraw = ImageDraw.Draw(testImg)
      return testDraw.textsize(txt, font)
  
  if __name__ == '__main__':
  
      fontname = &quot;Arial.ttf&quot;
      fontsize = 11   
      text = &quot;example@gmail.com&quot;
      
      colorText = &quot;black&quot;
      colorOutline = &quot;red&quot;
      colorBackground = &quot;white&quot;
  
  
      font = ImageFont.truetype(fontname, fontsize)
      width, height = getSize(text, font)
      img = Image.new('RGB', (width+4, height+4), colorBackground)
      d = ImageDraw.Draw(img)
      d.text((2, height/2), text, fill=colorText, font=font)
      d.rectangle((0, 0, width+3, height+3), outline=colorOutline)
      
      img.save(&quot;D:/image.png&quot;)
  </code></pre>