0

I have the following code (derived from this answer) 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)
Community
  • 1
  • 1
user2565679
  • 89
  • 1
  • 2
  • 8
  • Sorry, I think I caused a misunderstanding; you should not ask the whole question twice. Instead, show what you have tried, and include the full error message in the post. The image rendering, previous code, or my answer, is completely irrelevant here. – phihag Jul 11 '13 at 08:22
  • To be honest I don't know any python. I'll retry my edit and get all the information to let you guys know what I tried and what it did. Ill edit the main post. – user2565679 Jul 11 '13 at 17:34
  • Edited the main post, any help would be awesome. – user2565679 Jul 11 '13 at 20:43
  • Simplified the question. You can see the error if you open up a cmd window and then run the application manually. – phihag Jul 11 '13 at 20:46
  • Ok I edited my code a little bit, it prints the montage WITH the numbers, but they are too small. I am getting this error http://i.imgur.com/tqgUVfP.png When I only use the TTF font, but when I use try: image_font = ImageFont.truetype('font/Helvetica.ttf', 18) except: try: image_font = ImageFont.load('font/Helvetica-18.pil') except: image_font = ImageFont.load_default() it defaults to the last load default text – user2565679 Jul 11 '13 at 20:58
  • Added a preliminary answer. How and where did you install Python? There should also be more to that error; is that the only line in the output? – phihag Jul 11 '13 at 21:07

1 Answers1

1

You can open a command line window by pressing Win+rcmd 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 is probably because that your PIL has been compiled without TTF support.

Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469
  • Looks like it is working as intended now phihag. Thank you for your help on this journey. :P Do I post the completed code now or do I keep it as this? – user2565679 Jul 11 '13 at 21:11
  • No, please keep it as is, since that way, anyone else with the same problem can then benefit from the question and answer as well. – phihag Jul 11 '13 at 21:12
  • Okay sounds good. Thank you. Time to build it into a exe. :P – user2565679 Jul 11 '13 at 21:23