title: Drawing text in python
body: <p>I have the following code (derived from <a href="https://stackoverflow.com/a/17556210/35070">this answer</a>) with my attempt to add the text drawing of numbers.  It doesn't work.  It doesn't create an image and the cmd prompt is too fast to see which error it is throwing.</p>
  
  <pre><code>#!/usr/bin/env python
  import os.path
  import sys
  from time import strftime
  import Image
  import ImageDraw
  import ImageFont
  
  row_size = 3
  margin = 3
  
  def generate_montage(filenames, output_fn):
      images = [Image.open(filename) for filename in filenames]
  
      width = max(image.size[0] + margin for image in images)*row_size
      height = sum(image.size[1] + margin for image in images)
  
      montage = Image.new(mode='RGBA', size=(width, height), color=(0,0,0,255))
  
      image_font = ImageFont.truetype('font/Helvetica.ttf', 18)
      draw = ImageDraw.Draw(montage)
  
      max_x = 0
      max_y = 0
      offset_x = 0
      offset_y = 0
      for i,image in enumerate(images):
          montage.paste(image, (offset_x, offset_y))
  
          text_coords = offset_x + image.size[0] - 45, offset_y + 120
          draw.text(text_coords, '#{0}'.format(i+1), font=image_font)
  
          max_x = max(max_x, offset_x + image.size[0])
          max_y = max(max_y, offset_y + image.size[1])
  
          if i % row_size == row_size-1:
              offset_y = max_y + margin
              offset_x = 0
          else:
              offset_x += margin + image.size[0]
  
      montage = montage.crop((0, 0, max_x, max_y))
      montage.save(output_fn)
  
  if __name__ == '__main__':
      basename = strftime("Montage %Y-%m-%d at %H.%M.%S.png")
      exedir = os.path.dirname(os.path.abspath(sys.argv[0]))
      filename = os.path.join(exedir, basename)
      generate_montage(sys.argv[1:], filename)
  </code></pre>