Backend Development 10 min read

Generating QR Codes with the Python qrcode Library

This article explains how to install the qrcode module, generate QR codes using both functional and class‑based APIs, customize parameters such as version and error correction, produce PNG, SVG, and pure‑Python images, use the command‑line tool, and embed logos into QR codes with full code examples.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Generating QR Codes with the Python qrcode Library

Install the required module with pip install qrcode . The qrcode project on GitHub provides an interface for generating QR codes and uses the PIL library for image creation by default.

1. Function‑style QR code generation

Use qrcode.make(data) which returns a qrcode.image.pil.PilImage object. The object can be converted to a PIL Image via get_image() for displaying with show() or saved directly with save() .

import qrcode
img = qrcode.make("hello world!")
img.get_image().show()
img.save('hello.png')

2. Class‑instance style QR code generation

The built‑in QRCode class is instantiated as QRCode(version=None, error_correction=constants.ERROR_CORRECT_M, box_size=10, border=4, image_factory=None) . Data is added with add_data() , the image is created with make(fit=True) , and the final image object is obtained via make_image() .

version : integer 1‑40 (or string) controlling the matrix size; None lets the library choose the appropriate size when fit=True .

error_correction : level of error correction, using one of the constants: ERROR_CORRECT_L – ~7% error correction. ERROR_CORRECT_M – default, ~15% error correction. ERROR_CORRECT_Q – ~25% error correction. ERROR_CORRECT_H – ~30% error correction.

box_size : pixel size of each QR module (default 10).

border : number of modules for the white border (default 4, the minimum required).

image_factory : a subclass of qrcode.image.base.BaseImage that determines the image type returned by make_image() . Available factories are located in the image package (e.g., pil.py , pure.py , svg.py ).

Note: the make() function internally creates a QRCode instance, so you can also pass initialization parameters directly to make() .

The QRCode.make_image() method allows changing fill_color and back_color to customize foreground and background colors.

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data('Some data')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save('qr_image.png')

3. Generating SVG QR codes

The library can produce three SVG variants: a path‑based vector image, a full SVG composed of rectangles, and an SVG fragment (also rectangle‑based). The corresponding classes are SvgPathImage , SvgImage , and SvgFragmentImage in svg.py . Additional convenience factories SvgFillImage and SvgPathFillImage set a white background by default.

Example usage:

import qrcode
import qrcode.image.svg
if method == 'basic':
    factory = qrcode.image.svg.SvgImage
elif method == 'fragment':
    factory = qrcode.image.svg.SvgFragmentImage
else:
    factory = qrcode.image.svg.SvgPathImage
img = qrcode.make('Some data here', image_factory=factory)

Note: Python 2.6's xml.etree.ElementTree cannot generate SVG; install lxml for that functionality.

4. Pure‑Python PNG generation

After installing the pymaging and pymaging-png packages via:

pip install git+git://github.com/ojii/pymaging.git#egg=pymaging
pip install git+git://github.com/ojii/pymaging-png.git#egg=pymaging-png

you can generate PNGs with the pure‑Python factory:

import qrcode
from qrcode.image.pure import PymagingImage
img = qrcode.make('Some data here', image_factory=PymagingImage)

5. Command‑line mode

The package also provides a CLI tool. Use the qr command with optional --factory and --error-correction arguments:

qr "Some text" > test1.png
qr --factory=svg-path "Some text" > test2.svg
qr --factory=svg "Some text" > test3.svg
qr --factory=svg-fragment "Some text" > test4.svg
qr --factory=pymaging "Some text" > test5.png

6. Embedding a logo into a QR code

from PIL import Image
import qrcode
qr = qrcode.QRCode(version=5, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=8, border=4)
qr.add_data("http://www.cnblogs.com/sfnz/")
qr.make(fit=True)
img = qr.make_image().convert("RGBA")
icon = Image.open("D:/favicon.jpg")
img_w, img_h = img.size
factor = 4
size_w = int(img_w / factor)
size_h = int(img_h / factor)
icon_w, icon_h = icon.size
if icon_w > size_w:
    icon_w = size_w
if icon_h > size_h:
    icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w = int((img_w - icon_w) / 2)
h = int((img_h - icon_h) / 2)
icon = icon.convert("RGBA")
img.paste(icon, (w, h), icon)
img.save('D:/createlogo.jpg')

Summary

The qrcode module offers a versatile API for creating QR codes in PNG, SVG, and pure‑Python formats, supports command‑line generation, and allows extensive customization such as error correction levels, colors, and logo embedding, making it a convenient tool for developers.

References: Official qrcode documentation and source code; logo‑embedding example adapted from a Chinese blog.

pythonSVGcommand lineQR CodeImage Generationlogo embeddingQRCode library
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.