Instantly share code, notes, and snippets.

Embed
What would you like to do?
import glob
from PIL import Image, ImageDraw, ImageFont, PdfImagePlugin, _imaging ## was from PIL
from math import ceil
import os
badge_loc = 'border2.png' # badge template
filename = 'names3.txt' # text file with names
dpi = 300
count = 0
size = (int(8.5*dpi), int(11*dpi)) # Size of paper to print (8.5x11)
numNames = sum(1 for line in open(filename))
numPages = int(ceil(float(numNames)/6))
pg = 0
for line in open(filename):
firstname, lastname = line.split(' - ')
badge = Image.open(badge_loc)
width, height = badge.size
draw = ImageDraw.Draw(badge)
font = ImageFont.truetype('arial.ttf',75)
msg = 'October 21st - 23rd 2016'
w,h = draw.textsize(msg, font = font)
draw.text(((width-w)/2, 150), msg, (179, 36,36), font=font)
font = ImageFont.truetype('arial.ttf',200)
msg = firstname
w, h = draw.textsize(msg, font=font)
draw.text(((width - w)/2, (height-h)/2-50), msg, (179, 36, 36), font=font)
msg = lastname
w, h = draw.textsize(msg, font=font)
# Make font smaller for long last names
if w >= 1400:
font = ImageFont.truetype('arial.ttf',75)
w, h = draw.textsize(msg, font = font)
elif w >= 1100:
font = ImageFont.truetype('arial.ttf',100)
w, h = draw.textsize(msg, font = font)
elif w >= 900:
font = ImageFont.truetype('arial.ttf',150)
w, h = draw.textsize(msg, font = font)
elif w >= 750:
font = ImageFont.truetype('arial.ttf',175)
w, h = draw.textsize(msg, font = font)
draw.text(((width - w)/2, (height-h)/2+200), msg, (179, 36, 36), font=font)
if count == 0 :
blank_image = Image.new("RGB", size, "White")
blank_image.paste(badge, (0,0))
blank_image.save('test1.pdf','pdf')
elif count == 1:
blank_image.paste(badge, (width+125, 0))
elif count == 2:
blank_image.paste(badge, (0, height+200))
elif count == 3:
blank_image.paste(badge, (width+125, height+200))
elif count == 4:
blank_image.paste(badge, (0, 3300-height))
elif count == 5:
blank_image.paste(badge, (width+125, (3300-height)))
fname = str(pg)+".png"
blank_image.save(fname,'png')
if pg == numPages - 1 and count == (numNames%6)-1:
print 'in here!'
fname = str(pg)+".png"
blank_image.save(fname,'png')
if count == 5:
pg += 1
count = -1
count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment