Generating and Scanning QR Codes with Python: Practical Solutions for Warehouse Management and Check-ins

Python tutorial - IT technology blog
Python tutorial - IT technology blog

Why is QR Code Still “King” for Physical Identification?

If you’re looking for a way to manage thousands of items in a warehouse without spending tens of thousands of dollars on RFID scanners, QR Codes are your lifesaver. In projects I’ve worked on, from inventory management to event check-in apps, QR Codes always win due to their cost-effectiveness. An RFID tag costs between $0.20 – $1.00, while a QR Code is virtually free, costing only the price of paper and ink.

Regarding storage capacity, traditional barcodes can only hold about 20-25 characters. In contrast, a QR Code can store up to 7,089 digits or 4,296 alphanumeric characters. Most importantly, any modern smartphone can serve as a professional reader.

This article goes straight into handling QR Codes using Python. We won’t just generate images and leave it at that; we’ll integrate them into a real-world operational workflow.

Choosing the Optimal Implementation Method

Typically, there are two main approaches:

  • Using an Online API (like Google Charts): You send a request and receive an image link. This is fast but depends on the internet. If the API server fails or sensitive data is leaked, your system will face major issues.
  • Using Local Libraries (qrcode, pyzbar): All processing happens right on your machine. Response times are measured in milliseconds, it works offline, and you can easily customize logos or brand colors.

For systems requiring high stability like Warehouse Management Systems (WMS), I always prioritize the local approach. The qrcode library is very lightweight and specialized for generation. Meanwhile, pyzbar is famous for its high sensitivity, even when images are tilted or slightly blurry.

Detailed Implementation Guide

1. Environment Setup

Install the necessary libraries via pip. I’ll also use Pillow for image processing and opencv-python for real-time scanning via camera.

pip install qrcode[pil] pyzbar opencv-python

Note for Windows users: If you get a missing DLL error, you need to install the Visual C++ Redistributable. For Linux, don’t forget to install libzbar0 via apt-get for the library to function.

2. Generating Professional QR Codes

Let’s assume each item in the warehouse needs an identifier containing the product ID, import date, and batch code. The code below generates a QR code with the highest error correction level.

import qrcode

def create_warehouse_qr(data, filename="product_001.png"):
    qr = qrcode.QRCode(
        version=1, 
        error_correction=qrcode.constants.ERROR_CORRECT_H, # Allows up to 30% damage while remaining readable
        box_size=10,
        border=4,
    )
    qr.add_data(data)
    qr.make(fit=True)

    img = qr.make_image(fill_color="black", back_color="white")
    img.save(filename)
    print(f"Success: {filename}")

# Real-world data often uses pipe separators
raw_data = "SKU-9923|2023-12-01|ZONE-A"
create_warehouse_qr(raw_data)

When data becomes complex, I often use Regex to extract information after scanning. If you want to quickly test Regex patterns before putting them into code, try using the Regex Tester. This tool helps you instantly confirm if your pattern correctly captures the SKU.

3. Decoding QR Codes from Images

Once the label is applied, employees just need to take a photo for inventory entry. pyzbar helps us read the content from that image file easily.

from pyzbar.pyzbar import decode
from PIL import Image

def scan_qr_file(path):
    results = decode(Image.open(path))
    for obj in results:
        print(f"Content: {obj.data.decode('utf-8')}")
        print(f"Code type: {obj.type}")

scan_qr_file("product_001.png")

4. Real-time Camera Scanning System

This is the most common application for check-in counters. Instead of opening individual files, the system scans continuously via webcam. Every time a new code is detected, it automatically cross-references the data.

import cv2
from pyzbar.pyzbar import decode

def start_scanner():
    cam = cv2.VideoCapture(0)
    history = set()

    while True:
        _, frame = cam.read()
        for code in decode(frame):
            content = code.data.decode('utf-8')
            if content not in history:
                print(f"[CHECK-IN]: {content}")
                history.add(content)
                # Draw a confirmation box on the screen
                x, y, w, h = code.rect
                cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)

        cv2.imshow("Scanner", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'): break

    cam.release()
    cv2.destroyAllWindows()

Real-world Experience for System Stability

After many implementation projects, I’ve gathered 3 golden rules to minimize scanning errors:

  1. Prioritize Level H Error Correction: In warehouse environments, labels are prone to scratches or dust. Level H allows recovery of up to 30% of lost data, saving employees from repeated scanning attempts.
  2. Control Data Density: Don’t cram long paragraphs into a QR code. More text means smaller and denser squares, making it hard for cheap cameras to focus. It’s best to store just an ID or a shortened URL.
  3. Contrast is Key: Always print black codes on a white background. Avoid light colors or printing on glossy plastic surfaces, as glare will blind the reader.

Conclusion

Combining qrcode and pyzbar is the fastest way to digitize operational processes using Python. Whether you’re managing assets or event tickets, this duo performs exceptionally well in terms of both speed and accuracy. If you encounter difficulties installing zbar on Docker or Windows environments, feel free to leave a question below for support!

Share: