Skip to main content

Command Palette

Search for a command to run...

QR Code Generator with Python

Published
3 min read
QR Code Generator with Python

Introduction

QR codes are everywhere—from payment systems to event check-ins, business cards, and even restaurant menus. They provide a fast and reliable way to encode and share information in a scannable format. In this guide, you’ll learn how to build a QR Code Generator using Python, step by step.

We’ll cover:

  • What QR codes are

  • Why you'd use them

  • Required Python libraries

  • How to generate and save a QR code image

  • How to customize QR codes (colors, size, etc.)

  • Common use cases

By the end, you’ll have a working script that generates a QR code from any text, link, or data string.

What is a QR Code?

A QR (Quick Response) code is a 2D barcode that can store a wide range of data—URLs, phone numbers, contact info, and even Wi-Fi credentials. It’s machine-readable and can be scanned using mobile devices or webcams.

Unlike traditional barcodes, QR codes:

  • Store much more data

  • Can be scanned from any angle

  • Include error correction for partial damage

Why Build a QR Code Generator?

Some practical uses for generating QR codes include:

  • Sharing your portfolio link on business cards

  • Encoding secure Wi-Fi credentials

  • Linking to product pages or forms

  • Creating educational handouts with embedded links

  • Adding links to digital resources in printed material

Step 1: Install Required Libraries

We’ll use the qrcode and Pillow libraries. Install them with pip:

pip install qrcode[pil]

qrcode handles QR code creation, and Pillow is used to generate the image output.

Step 2: Import Necessary Modules

import qrcode

We only need the qrcode module for basic functionality. It includes everything needed to generate and save a QR code image.

Step 3: Define the Data You Want to Encode

data = "https://example.com"

This is the text or link you want the QR code to represent. It could be a URL, plain text, contact info, etc.

Step 4: Create a QR Code Object

qr = qrcode.QRCode(
    version=1,  # Controls the size of the QR Code: 1 is 21x21
    error_correction=qrcode.constants.ERROR_CORRECT_H,  # High error correction
    box_size=10,  # Size of each box in pixels
    border=4,  # Border size (4 is the minimum)
)
qr.add_data(data)
qr.make(fit=True)

Explanation:

  • version: Higher values create larger QR codes to hold more data.

  • error_correction: Ranges from L (7%) to H (30%) redundancy. Higher allows the code to work even if partially damaged.

  • box_size: Pixel size of each square in the QR code.

  • border: Margin size around the QR code.

Step 5: Generate and Save the QR Code Image

img = qr.make_image(fill_color="black", back_color="white")
img.save("qrcode.png")
  • fill_color sets the foreground (data) color.

  • back_color sets the background.

  • The image is saved as qrcode.png in the current directory.

Full Working Script

import qrcode

# Step 1: Define the data to encode
data = "https://example.com"

# Step 2: Create a QR Code object
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=10,
    border=4,
)

# Step 3: Add data and make the QR code
qr.add_data(data)
qr.make(fit=True)

# Step 4: Create the image and save it
img = qr.make_image(fill_color="black", back_color="white")
img.save("qrcode.png")

You can now open the qrcode.png file and scan it using any QR scanner app.

Conclusion

Creating a QR code generator in Python is both simple and powerful. With just a few lines of code, you can generate QR codes for virtually any kind of data. Whether you're building a tool for your business, personal use, or a larger automation system, Python’s qrcode module gives you flexibility, customization, and ease of use.

As a next step, you could:

  • Add a GUI using tkinter

  • Automate QR creation for multiple URLs from a file

  • Create batch PDFs of QR codes for printing

With this foundation, you're well on your way to using QR codes creatively and efficiently in your own projects.