Drawing text in python
Question: I have the following code (derived from [this
answer](http://stackoverflow.com/a/17556210/35070)) 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.

    
    
    #!/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)
    


Answer: You can open a command line window by pressing `Win`+`r``cmd` `Enter`. Once in
there, you execute your program and still see its output. Another option would
be to wrap the `generate_montage` call, like this:

    
    
    try:
        generate_montage(sys.argv[1:], filename)
    except:
        import traceback,time
        traceback.print_exc()
        time.sleep(600)
    

In any case, the most likely problem is that the font isn't being found, as
you're loading it from a directory relative to the cwd. Pass in the base
directory, like this:

    
    
    base = os.path.dirname(os.path.abspath(__file__))
    try:
        fn = os.path.join(base, 'font', 'Helvetica.ttf')
        image_font = ImageFont.truetype(fn, 18)
    except:
        try:
            fn = os.path.join(base, 'font', 'Helvetica-18.pil')
            image_font = ImageFont.load(fn)
        except:
            image_font = ImageFont.load_default()
    

The reason that TTF font loading [doesn't
work](http://i.imgur.com/tqgUVfP.png) is probably because that [your PIL has
been compiled without TTF support](http://stackoverflow.com/a/4011715/35070).

