#!/usr/bin/python3

from PIL import Image, ImageFont, ImageDraw
import os
import subprocess
import shutil
import sys

directory = "output"
text = sys.argv[1]
outfile = sys.argv[2]

fnt = ImageFont.truetype('Beeb/Beeb.ttf', 8)

(pixels, height) = fnt.getsize(text)

# Lead-in plus lead-out
frames = pixels + 8 + 8

image = Image.new('L', (frames, 8), 0)
draw = ImageDraw.Draw(image)

draw.text((8, 0), text, 255, fnt)

os.mkdir(directory)

for frame in range(0,frames):
    img = image.crop((frame, 0, frame+8, 8))
    img.save("%s/%04d.png" % (directory, frame))

subprocess.call(["convert", "%s/*.png" % directory, outfile])
shutil.rmtree(directory)
