Skip to content

Instantly share code, notes, and snippets.

@bkeating
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkeating/2f87575a4633236734df to your computer and use it in GitHub Desktop.
Save bkeating/2f87575a4633236734df to your computer and use it in GitHub Desktop.
Generate a range of random 'codes' for Santa on the Square photo service
import random
import string
import subprocess
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
codes = []
for i in range(1, 251):
char_pool = string.ascii_uppercase + string.digits
# Generate a random code
r = ''.join([random.choice(char_pool) for n in xrange(5)])
# Check generated code against pre-existing codes.
if r not in codes:
codes.append(r)
# Open template image and draw on it
img = Image.open("template.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("Inconsolata.otf", 72)
draw.text((430, 527), str(i), (0,0,0), font=font)
draw.text((430, 706), str(r), (0,0,0), font=font)
# save
img.save('./cards/%02d%s.png' % (i, r))
print "Created card for #%s, Code: %s" % (i, r)
print "Merging all cards into a single PDF..."
subprocess.call('convert ./cards/* output.pdf', shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment