Generating Unique Event Passes

unique QR tickets with custom design using Python

Generating Unique Event Passes

Introduction

In this blog post, we will explore the process of creating indigenous and independent QR-based tickets for your event, be it a festival or any other gathering where passes are required for entry. The objective is to generate tickets with unique QR codes for public distribution while implementing measures to safeguard against duplication. This discussion aims to provide comprehensive insights into the effective management of event access through personalised QR ticketing systems.

Abstract

We need to implement the given idea in the following ways:

  1. Create Unique QR codes

  2. Save the QR Code Data in cloud storage

  3. Place the QR Code Image in desired place in the Pass Design

Create Unique QR Codes

we are going to use the qrcode library to create different QR codes with specific data. One can install the qrcode library using the command pip3 install qrcode

Then you need to create the QR code using the qrcode.make(<DATA>) function and then save the QR code image in local storage, you can refer to the below mentioned example

import qrcode
qr = qrcode.make('Swoyam Siddharth Nayak')
qr.save("myQr.png")

Now we need to pass unique data to the qrcode.make() function to create unique QRs, for the same we would take the leverage of random library of python and generate unique random numbers and place them in QR image.

import random
import qrcode
num = random.randint(0,999999999999)
data = "ticket_no:"+str(num)
qr = qrcode.make(data)
qr.save(qrCode.png)

The provided code snippet serves the purpose of generating distinct data points in the form of ticket numbers, each prefixed with ticket_no: followed by a random numerical value ranging from 0 to 999999999999. While this method significantly minimizes the likelihood of producing duplicate tickets, we will address this issue comprehensively in subsequent steps when implementing data storage on cloud platforms. This strategic approach ensures a robust solution for mitigating the potential occurrence of duplicate tickets.

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')

The above code will repeat the above processes for 100 times and will generate 100 different unique QR Codes.

Save the QR Code Data in cloud storage

Following the successful generation of QR codes, the subsequent imperative is to securely store these codes in an online cloud database. This strategic measure is essential to facilitate seamless utilization of the QR codes for various purposes, including but not limited to event check-ins and related services. The adoption of an online cloud database ensures a reliable and accessible repository for the efficient management and utilization of the generated QR codes in subsequent event-related processes.

For this example, we would be using mongoDB Atlas for storing the QR data online, following code snippet shows how we are going to save the QR Data in MongoDB Atlas database

from pymongo import MongoClient
client = MongoClient('MONGO_URL')
db = client['<databse_name>']

import qrcode
import random
for i in range(100):
    num = random.randint(0, 9999999999999999999)
    data = f'ticket_no:{str(num)}'
    temp = db.tickets.find_one({"qr": data})
    if(temp):
        print("Duplicate Data Generated")
        i-=1
        continue
    else:
        qr = qrcode.make(data)
        qr.save(f'./result/myQr{str(i)}.png')
        db.tickets.insert_one({"qr": data, "checkIn": False})

IIn the provided code excerpt, we establish a connection to an online MongoDB cluster using the MongoClient from the pymongo library in Python. Subsequently, upon generating unique data for the QR codes, a validation check is performed to ascertain whether the generated code already exists through the use of the randint() function.

In the event that the code has been previously generated, the subsequent processes are bypassed. Conversely, if the code is deemed unique, the system proceeds to generate the corresponding QR code image. This image is then saved in local storage, and the associated unique data is stored in the database via the designated function: db.tickets.insert_one({"qr": data, "checkIn": False}). This meticulous process ensures the integrity of the generated QR codes and their seamless integration into the database for subsequent utilization.

Placing the QR Code Image

in this example, we are going to join two images horizontally. Firstly we need to create our image that we need to attach horizontally

For illustration purposes, should the necessity arise to position the aforementioned image to the right side of our QR Code, the recommended approach involves placing this image within the directory where the script is located. It is advisable to assign a unique and discernible name to the image, facilitating easy access for subsequent use. In this particular example, we designate the image as pass.png. This judicious naming convention ensures clear identification and accessibility within the script's working directory.

Now we would use the Pillow PIL library of python for joining the two images horizontally

from PIL import Image
import qrcode
qr = qrcode.make('<QR-Code-Data>')
qr.save("myQr.png")
img1 = Image.open("myQr.png")
img2 = Image.open("./pass.png")
w1, h1 = img1.size
w2, h2 = img2.size
newHeight = max(h1, h2)
newWidth = w1 + w2
newImage = Image.new('RGB', (newWidth, newHeight), (255, 255, 255))
newImage.paste(img1, (0, (newHeight-h1)//2))
newImage.paste(img2, (w1, (newHeight-h2)//2))
newImage.save("result.png")

In the provided example, a distinctive QR code is generated and saved as myQr.png while the remaining pass elements are stored as pass.png Employing the Image module from the PIL Library, a deliberate and precise process is executed. This involves horizontally pasting the generated QR code and the pre-existing pass image onto a blank canvas with a white background. This methodical approach ensures the seamless integration of the QR code and pass elements into a cohesive visual representation.

The above is an example of the result ticket that we receive upon successful generation of the ticket

Conclusion

from pymongo import MongoClient
import qrcode
from PIL import Image
import random
import mongo

client = MongoClient('MONGO_URL')
db = client['test']


n = int(input("Enter the number of Tickets codes you want to generate: "))
for i in range(n):
    # Generating the qr code
    num = random.randint(0, 9999999999999999999)
    num2 = random.randint(0, 9999999999999999999)
    qr = qrcode.make(f'{str(num2)}advaita2024{str(num)}')
    qr.save(f'./result/myQr{str(num)+str(num2)}.png')
    db.tickets.insert_one(
        {"qr": f'{str(num2)}advaita2024{str(num)}', "checkIn": False})
    # joining the two images
    img1 = Image.open(f'./result/myQr{str(num)+str(num2)}.png')
    img2 = Image.open("./pass.png")
    w1, h1 = img1.size
    w2, h2 = img2.size
    newHeight = max(h1, h2)
    newWidth = w1 + w2
    newImage = Image.new('RGB', (newWidth, newHeight), (255, 255, 255))
    newImage.paste(img1, (0, (newHeight-h1)//2))
    newImage.paste(img2, (w1, (newHeight-h2)//2))
    newImage.save(f'./output/myQr{str(num)+str(num2)}.png')
    print(f"Ticket {i+1} Generated")

In conclusion, this blog has illuminated a comprehensive approach to creating bespoke QR-based tickets for events, offering a blend of uniqueness and security. Leveraging the qrcode library in Python, we have explored the generation of distinctive QR codes with randomized data, significantly reducing the risk of duplications.

Taking a step further, we addressed the importance of securely storing these QR codes in a cloud database, exemplifying the process using MongoDB Atlas. This ensures not only the integrity of the ticketing system but also opens avenues for streamlined event management, including check-ins and related services.

The blog also delved into the visual aspects of ticket design, showcasing the seamless integration of QR codes and pass images using the Pillow PIL library. This meticulous approach not only enhances the aesthetic appeal of the tickets but also provides organizers with a versatile tool for crafting visually impactful event passes.

In essence, this blog equips event organizers with the knowledge and tools necessary to create, manage, and visually enhance QR-based tickets, contributing to a more efficient, secure, and visually appealing event experience.