How to Generate Unique QR Code Event Passes with Python

How To Generate Unique Event Passes with Python

Event organizers require robust access management systems to facilitate entry for attendees securely. Traditional paper ticketing brings risks like counterfeiting, duplication, and lack of access controls after first use. Modern solutions like QR code-based digital passes provide enhanced security, better analytics, and more control.

This article provides a step-by-step tutorial on leveraging Python to create unique QR code passes for events of any scale. It covers the end-to-end process – from generating randomized QR codes to seamlessly integrating them with custom pass designs – focusing on simplicity and beginner-friendliness.

Generating Unique QR Codes

The first step is generating QR codes programmatically with randomized data strings to ensure uniqueness for each pass. We’ll use the Python qrcode library:

    import qrcode
    import random

    for i in range(100):
      num = random.randint(0, 9999999999999999999)
      data = f'ticket_no:{str(num)}' 
      qr = qrcode.make(data)'./result/myQr{str(i)}.png')

This creates 100 QR codes with ticket numbers between 0 and 999 trillion, saving each QR code image with a sequential file name.

We add validity checks to ensure no duplicate codes get generated:

    import pymongo

    client = MongoClient(MONGO_URL) 
    db = client['<database_name>']

    for i in range(100):
      num = random.randint(0, 9999999999999999999)
      data = f'ticket_no:{str(num)}'

      # Check if QR code exists
      temp ={"qr": data}) 
        print("Duplicate QR Code")

      # Generate and save QR
      qr = qrcode.make(data){"qr": data, "checkIn": False}) 

This connects to a MongoDB database that checks if the randomly generated code exists before creating and storing the QR code data.

Integrating QR Codes into Ticket Design

Most tickets require integrating the QR code into a customized graphic design featuring event details, branding elements, etc.

We leverage Python’s PIL library to merge the visual ticket design programmatically and generate QR code into a single ticket image:

    from PIL import Image

    qr = qrcode.make('<QR_CODE_DATA>')"myQr.png")

    img1 ="myQr.png")  
    img2 ="./ticket_design.png")
    # Create canvas matching max width and height 
    new_height = max(img1.height, img2.height)
    new_width = img1.width + img2.width  
    new_image ='RGB', (new_width, new_height))

    # Paste images side by side
    new_image.paste(img1, (0,0)) 
    new_image.paste(img2, (img1.width, 0))"final_ticket.png")

This script opens the QR code and background ticket design, identifies optimum dimensions, and pastes both images side-by-side into the final ticket file.

Scaling Ticket Generation

Wrapping the above procedures into functions lets you rapidly generate any number of uniquely coded tickets in batch while dynamically customizing elements like ticket design, text, etc:

    for i in range(100):

      # Generate QR Code
      qr_data = generate_qr() 

      # Create Ticket
      create_ticket(qr_data, design, text) 

Adding parameterized inputs, validity checks, and error handling provides further control and automation possibilities.


This guide provided a beginner-friendly introduction to leveraging Python to create foolproof digital event passes. Automating uniqueness and integrating custom designs help scale secure access management to events of any size without compromising branding or user experience.

As physical paper tickets get increasingly phased out in favor of scannable QR code-based solutions, having these skills as an event organizer can help future-proof and streamline attendee access control while unlocking creative possibilities for community engagement.

If you like my work and want to help me continue dropping content like this, buy me a cup of coffee.
If you find this post exciting, find more exciting posts on Learnhub Blog; we write everything tech from Cloud computing to Frontend DevCybersecurityAI, and Blockchain.


Leave a Comment

Your email address will not be published. Required fields are marked *