Fundamentals 7 min read

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

This guide walks you through using the Python qrcode library, covering project setup, QRCode class parameters, method details, SVG and PNG image generation options, and provides practical code snippets to create and customize QR codes efficiently.

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

Project address

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

Import statement

import qrcode

QRCode class usage

Instantiate a QRCode object with the desired configuration:

qr = 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 yields a 21×21 matrix; None or fit=True lets the library choose the smallest size automatically.

error_correction : Sets error‑correction level.

ERROR_CORRECT_L – ~7% error correction.

ERROR_CORRECT_M – default, ~15%.

ERROR_CORRECT_Q – ~25%.

ERROR_CORRECT_H – ~30%.

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

border : Width of the white border in modules (default 4).

image_factory : Chooses the image backend (default is PIL).

mask_pattern : Selects the mask pattern for the QR code.

Common methods

add_data(data, optimize=20)

: Add text to be encoded; optimize splits data into chunks for a more compact QR code. make(fit=True): Automatically selects the smallest version that fits the data; also chooses an optimal mask.

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

: Returns an image object (PIL by default). Set fill_color and back_color for colored QR codes. 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 an ASCII representation (mostly for debugging).

SVG image generation

The library can produce three SVG variants via image_factory: SvgPathImage: Vector paths – scalable without quality loss. SvgImage: Collection of rectangles forming a complete SVG file (may show gaps when zoomed). SvgFragmentImage: Rectangle collection suitable as an SVG fragment.

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 image generation

To use the optional pymaging backend, install it via:

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

Then pass qrcode.image.pure.PymagingImage as image_factory:

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

In most cases the default PIL backend is sufficient:

# Display the image
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.

SVGimage generationPNGqrcodeQRCode library
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.