Skip to content

Instantly share code, notes, and snippets.

@azimjohn
Created July 13, 2019 12:38
Show Gist options
  • Save azimjohn/fce86f5348e28d5345575d99f00ab2ce to your computer and use it in GitHub Desktop.
Save azimjohn/fce86f5348e28d5345575d99f00ab2ce to your computer and use it in GitHub Desktop.
Generate ticket for event
from PIL import Image, ImageFont, ImageDraw # Pillow==6.0.0
WIDTH = 480
HEIGHT = 640
def get_font(size, is_bold=False):
"""https://fonts.google.com/specimen/Open+Sans"""
if is_bold:
return ImageFont.truetype('OpenSans-Bold.ttf', size)
return ImageFont.truetype('OpenSans-Light.ttf', size)
def generate_ticket(logo, ticket_id, event_name, event_datetime, registrant_name, contact):
ticket = Image.new('RGB', (WIDTH, HEIGHT), 'white')
draw = ImageDraw.Draw(ticket)
ticket_font = get_font(32)
event_name_font = get_font(28, is_bold=True)
event_datetime_font = get_font(18)
registrant_name_font = get_font(28, is_bold=True)
branch_name_font = get_font(18)
contact_font = get_font(18)
ticket_width, _ = ticket_font.getsize(ticket_id)
draw.text((WIDTH-ticket_width-12, 8), ticket_id, font=ticket_font, fill="black")
logo_width, _ = logo.size
margin = int((WIDTH - logo_width) / 2)
ticket.paste(logo, (margin, 120))
draw.text((margin, 350), event_name, font=event_name_font, fill="black")
draw.text((margin, 385), event_datetime, font=event_datetime_font, fill="black")
draw.text((margin, 465), registrant_name, font=registrant_name_font, fill="black")
draw.text((margin, 500), contact, font=branch_name_font, fill="black")
return ticket
if __name__ == '__main__':
logo = Image.open('logo.png')
image = generate_ticket(
logo, "#1231", "How to make money", "May 7, 14:30", "Abdullaev Sarvar", "TexnoPlaza, +99890 871 23 87"
)
image.show()
# image.save('ticket.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment