Skip to content

Instantly share code, notes, and snippets.

@harisibrahimkv
Created September 22, 2015 02:00
Show Gist options
  • Save harisibrahimkv/74751dc91a633bf19acb to your computer and use it in GitHub Desktop.
Save harisibrahimkv/74751dc91a633bf19acb to your computer and use it in GitHub Desktop.
import os
import sys
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def make_qr_jpg(png_image):
bg = Image.new("RGB", png_image.size, (0, 0, 0))
bg.paste(png_image, png_image)
bg.save('qr.jpg')
return
def get_resized_qr(image):
size = 400, 400
return image.resize(size, Image.ANTIALIAS)
def main():
try:
directory_name = sys.argv[1]
except:
print "Please specify the directory name"
destination_dir = 'badges_with_names'
os.mkdir(destination_dir)
W, H = (1200, 1800)
files = os.listdir(directory_name)
for a_file in files:
name = a_file.split('_')[0].title()
ticket_no = a_file.split('_')[1][:-4]
badge = Image.open('badge.jpg')
qr_png = Image.open(directory_name+"/"+a_file)
make_qr_jpg(qr_png)
qr_jpg = Image.open('qr.jpg')
qr_jpg = get_resized_qr(qr_jpg)
badge.paste(qr_jpg, (400, 400), 0)
draw = ImageDraw.Draw(badge)
font_name = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeSerif.ttf', 75)
font_ticket = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeSerif.ttf', 45)
# Write the name
w, h = draw.textsize(name, font_name)
draw.text(((W - w) / 2, 800), name, (0, 0, 0), font=font_name)
# Write the ticket Number
w, h = draw.textsize(ticket_no, font_ticket)
draw.text(((W - w) / 2, 900), ticket_no, (0, 0, 0), font=font_ticket)
destination_file = destination_dir + "/" + a_file[:-4] + ".jpg"
badge.save(destination_file)
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment