Master Python QR Code Generation: Parameters, Methods, and Image Formats

This guide explains how to use the Python qrcode library, covering project setup, QRCode constructor parameters, common methods, and how to generate both SVG and PNG images with code examples and configuration details.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python QR Code Generation: Parameters, Methods, and Image Formats

Project URL

https://github.com/lincolnloop/python-qrcode

Import

import qrcode

Usage

QRCode constructor

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 QR code size (1‑40). Version 1 produces a 21×21 matrix; None or fit=True lets the library choose the optimal size.

error_correction: sets error‑correction level (L≈7%, M≈15%, Q≈25%, H≈30%).

box_size: pixel size of each matrix module, default 10.

border: number of modules for the quiet zone around the QR code, default 4.

image_factory: selects the image backend, default is a PIL image.

mask_pattern: chooses a mask pattern for the QR code.

Common methods

add_data(data, optimize=20): adds text to be encoded; set optimize=0 to disable optimization.

make(fit=True): automatically selects the smallest version and mask that fit the data.

make_image(fill_color=None, back_color=None, image_factory=None): creates and returns an image (default PIL).

clear(): clears the stored data.

get_matrix(): returns the QR code matrix.

print_ascii(out=None, tty=False, invert=False): prints an ASCII representation of the QR code.

SVG output

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

import qrcode.image.svg as svg
factory = svg.SvgImage  # or svg.SvgPathImage, svg.SvgFragmentImage
img = qrcode.make('Some data here', image_factory=factory)
SVG QR code example
SVG QR code example

PNG output

For PNG generation you can use the optional Pymaging backend. Install it via pip and use PymagingImage as the image factory. The default PIL backend works for most cases.

pip install git+https://github.com/ojii/pymaging.git#egg=pymaging
pip install git+https://github.com/ojii/pymaging-png.git#egg=pymaging-png
from qrcode.image.pure import PymagingImage
img = qrcode.make('Some data here', image_factory=PymagingImage)

Simple make usage

# Display the QR code
qrcode.make('hello world!').show()
# Save as PNG
qrcode.make('hello world!').save('hello.png')
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.

qrcodeimage-outputqr-code-generation
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.