Python  

Generating QR Codes Using Python: A Practical Technical Guide

1. Introduction

QR (Quick Response) codes are widely used for encoding information such as URLs, asset IDs, contact details, and structured data. In modern enterprise applications—especially in field service, automation, and asset tracking systems—QR codes provide a fast and reliable way to bridge physical and digital systems.

This article explains how to generate QR codes using Python, customize them, and apply them in real-world scenarios.

2. Prerequisites

  • Python 3.x installed

  • Basic understanding of Python

  • Virtual environment (recommended)

3. Required Libraries

We will use:

  • qrcode - for generating QR codes

  • Pillow (PIL) - for image processing

Installation

pip install "qrcode[pil]"

4. Basic QR Code Generation

The simplest way to generate a QR code:

import qrcode

data = "https://example.com"

img = qrcode.make(data)
img.save("qrcode.png")

How it works:

  • Input data is encoded into a QR matrix

  • The library converts it into an image

  • Output is saved as a PNG file

5. Custom QR Code Generation

For better control, use the QRCode class:

import qrcode

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=10,
    border=4,
)

qr.add_data("AssetID: A101")
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
img.save("custom_qr.png")

Key Parameters

ParameterDescription
versionControls size (1–40)
error_correctionError tolerance (L, M, Q, H)
box_sizePixel size of each box
borderWhite border thickness

6. Encoding Structured Data (JSON)

QR codes can store structured data:

import json
import qrcode

data = json.dumps({
    "AssetID": "A101",
    "Location": "Plant1",
    "Type": "Pump"
})

img = qrcode.make(data)
img.save("json_qr.png")

This is especially useful for enterprise applications.

7. Adding Text Below QR Code

To label QR codes (e.g., asset names), use Pillow:

import qrcode
from PIL import Image, ImageDraw, ImageFont

data = "AssetID: A101"
label = "Pump A101"

qr = qrcode.make(data).convert("RGB")

width, height = qr.size
new_img = Image.new("RGB", (width, height + 40), "white")
new_img.paste(qr, (0, 0))

draw = ImageDraw.Draw(new_img)
font = ImageFont.load_default()

text_width, text_height = draw.textsize(label, font=font)
text_x = (width - text_width) // 2

draw.text((text_x, height + 5), label, fill="black", font=font)

new_img.save("qr_with_label.png")

8. Adding a Logo to QR Code

You can embed a logo in the center:

from PIL import Image
import qrcode

qr = qrcode.make("https://example.com").convert("RGB")
logo = Image.open("logo.png").resize((80, 80))

pos = ((qr.size[0] - 80) // 2, (qr.size[1] - 80) // 2)
qr.paste(logo, pos)

qr.save("qr_with_logo.png")

Use high error correction (ERROR_CORRECT_H) to ensure scannability.

9. Bulk QR Code Generation

For multiple assets:

import qrcode

items = ["A101", "A102", "A103"]

for item in items:
    img = qrcode.make(f"AssetID:{item}")
    img.save(f"{item}.png")

10. Real-World Use Cases

10.1 Field Service & Asset Tracking

  • Generate QR codes for equipment

  • Scan to retrieve maintenance history

  • Link to digital forms

10.2 Document Management

  • Encode SharePoint or document URLs

  • Enable quick access via mobile scanning

10.3 Inventory Systems

  • Track items using QR labels

  • Integrate with backend APIs

11. Conclusion

Python makes QR code generation simple, flexible, and scalable. With libraries like qrcode and Pillow, developers can:

  • Generate dynamic QR codes

  • Customize design and content

  • Integrate with enterprise systems

For modern applications—especially in automation, field service, and digital transformation—QR codes serve as a critical bridge between physical assets and digital platforms.