Master Python QR Code Generation: Guide to qrcode Parameters & Image Types

This article provides a comprehensive tutorial on using the Python qrcode library, covering installation, the QRCode class constructor, detailed explanations of all parameters, common methods, and how to generate both SVG and PNG images, including code examples and tips for customizing output.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python QR Code Generation: Guide to qrcode Parameters & Image Types

Project address https://github.com/lincolnloop/python-qrcode Import import qrcode Usage

QRCode method

qrcode.QRCode(
    version=1,
    error_correction=qrcode.ERROR_CORRECT_L,
    box_size=10,
    border=4,
    image_factory=None,
    mask_pattern=None
)

Parameter explanations

version: controls the QR code size (1‑40). Value 1 yields a 21×21 matrix; None or fit=True lets the library choose the smallest size automatically.

error_correction: level of error correction (L≈7% errors corrected, M≈15% default, Q≈25%, H≈30%).

box_size: pixel size of each module, default 10.

border: number of modules for the white border, default 4.

image_factory: selects the image backend, default is PIL.

mask_pattern: selects the mask pattern used during generation.

Common methods add_data(str, optimize=20): add data to be encoded; set optimize=0 to disable optimization. make(fit=True): automatically chooses the smallest version and mask; does not return a value.

make_image(fill_color=None, back_color=None, image_factory=None)

: creates and returns an image (default PIL); colors can be customized. clear(): clears the stored data. get_matrix(): returns the QR code matrix as a list of lists. print_ascii(out=None, tty=False, invert=False): prints the QR code as ASCII art.

SVG generation

qrcode can produce three SVG variants: a path‑based vector image, a full rectangle‑based SVG, and a rectangle fragment. They correspond to SvgPathImage, SvgImage, and SvgFragmentImage in svg.py. Pass the desired class as image_factory when calling qrcode.make or creating a QRCode instance.

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)

PNG generation

Optional pymaging and pymaging-png packages allow using PymagingImage as image_factory for PNG output, but the default PIL backend already creates PNG files.

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

make method

The make function internally creates a QRCode instance and calls make_image(), returning a PIL image by default.

# Show image
qrcode.make('hello world!').show()
# Save PNG
qrcode.make('hello world!').save('hello.png')

run_example

Generates a QR code for the author's project website and displays it.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonSVGQR codeimage generationPNGqrcode
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.