DEV Community

Cover image for How to Generate Unique QR Code Event Passes with Python
Scofield Idehen
Scofield Idehen

Posted on • Originally published at blog.learnhub.africa

How to Generate Unique QR Code 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)
      qr.save(f'./result/myQr{str(i)}.png')
Enter fullscreen mode Exit fullscreen mode

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 = db.tickets.find_one({"qr": data}) 
      if(temp):
        print("Duplicate QR Code")
        continue 

      # Generate and save QR
      qr = qrcode.make(data)
      db.tickets.insert_one({"qr": data, "checkIn": False}) 
Enter fullscreen mode Exit fullscreen mode

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>')
    qr.save("myQr.png")

    img1 = Image.open("myQr.png")  
    img2 = Image.open("./ticket_design.png")
Enter fullscreen mode Exit fullscreen mode
    # Create canvas matching max width and height 
    new_height = max(img1.height, img2.height)
    new_width = img1.width + img2.width  
    new_image = Image.new('RGB', (new_width, new_height))

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

    new_image.save("final_ticket.png")
Enter fullscreen mode Exit fullscreen mode

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) 
Enter fullscreen mode Exit fullscreen mode

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

Conclusion

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 Dev, Cybersecurity, AI, and Blockchain.

Resource

Top comments (0)