Quickstart: Generate QR code in Python
mainUse the qrcode.make() shortcut function for simple QR code generation.
import qrcode
img = qrcode.make('Some data here')
type(img) # qrcode.image.pil.PilImage
img.save("some_file.png")repository·main·Indexed 26 days ago
https://github.com/lincolnloop/python-qrcodeA pure Python QR Code generator (version 8.2) capable of creating QR codes in PNG, SVG, and ASCII formats. It provides a high-level `make()` shortcut, a granular `QRCode` class for configuring version, box size, and border, and a `qr` command-line interface. The library supports various error correction levels (L, M, Q, H) and advanced styling via `StyledPilImage`, including rounded modules, gradients, and embedded images.
Use the qrcode.make() shortcut function for simple QR code generation.
import qrcode
img = qrcode.make('Some data here')
type(img) # qrcode.image.pil.PilImage
img.save("some_file.png")To install the basic version of qrcode (which includes a pure Python PNG encoder using pypng and console rendering support), run:
pip install qrcodeFor advanced image functionality, including support for Pillow (PIL) to generate high-quality images, install with the pil dependency:
pip install "qrcode[pil]"pip install qrcode
pip install "qrcode[pil]"To develop or run tests for this project, you need to install uv for dependency management and just as a command runner.
uv.just.To run all project checks, including formatting, linting, and tests, use the check command via just.
just checkjust to manage test execution. You can run the full suite, a quick subset, or target specific Python versions and image factory variants.The add_data() method appends data to the current QRCode object. If you want to replace the existing content with new data in the same object, you must call clear() first.
import qrcode
qr = qrcode.QRCode()
qr.add_data('Some data')
img = qr.make_image()
# Replace data
qr.clear()
qr.add_data('New data')
other_img = qr.make_image()For highly customized QR codes (rounded corners, gradients, or embedded images), use StyledPilImage. This requires the pillow dependency.
module_drawer: Controls the shape of the QR modules (e.g., RoundedModuleDrawer, CircleDrawer).color_mask: Changes the colors of the QR code (e.g., RadialGradiantColorMask).embedded_image_path: Path to an image to embed in the center of the QR code.Note: Styled QR codes are not guaranteed to work with all readers. It is recommended to use ERROR_CORRECT_H when using styles or embedded images.
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
from qrcode.image.styles.colormasks import RadialGradiantColorMask
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
qr.add_data('Some data')
# Example: Rounded corners, radial gradient, and embedded image
img = qr.make_image(
image_factory=StyledPilImage,
module_drawer=RoundedModuleDrawer(),
color_mask=RadialGradiantColorMask(),
embedded_image_path="/path/to/image.png"
)
img.save("styled_qr.png")To generate SVG files in Python, you must specify an image_factory from qrcode.image.svg.
qrcode.image.svg.SvgImage: Simple factory (set of rectangles).qrcode.image.svg.SvgFragmentImage: Fragment factory (no standalone header).qrcode.image.svg.SvgPathImage: Combined path factory (recommended; fixes white space issues when zooming).qrcode.image.svg.SvgFillImage: SVG factory that fills the background with white.qrcode.image.svg.SvgPathFillImage: SVG path factory that fills the background with white.The make_image() method forwards keyword arguments to the underlying ElementTree XML library via the attrib parameter.
import qrcode
import qrcode.image.svg
# Using the Path factory
qr = qrcode.QRCode(image_factory=qrcode.image.svg.SvgPathImage)
qr.add_data('Some data')
qr.make(fit=True)
# Adding custom XML attributes
img = qr.make_image(attrib={'class': 'some-css-class'})
img.save('some_file.svg')
# Converting to string
svg_string = img.to_string(encoding='unicode')For granular control over the QR code properties, use the qrcode.QRCode class.
version: Integer from 1 to 40 (size of the matrix). Set to None and use fit=True in make() to determine automatically.error_correction: Controls error correction capability. Use constants from qrcode.constants:ERROR_CORRECT_L: ~7% error correction.ERROR_CORRECT_M: ~15% error correction (default).ERROR_CORRECT_Q: ~25% error correction.ERROR_CORRECT_H: ~30% error correction.box_size: Controls how many pixels each "box" of the QR code is.border: Controls the thickness of the border (default is 4).When using the default image factory, you can pass fill_color and back_color (accepting RGB tuples) to make_image().
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)
# Using RGB tuples for colors
img = qr.make_image(fill_color=(55, 95, 35), back_color=(255, 195, 235))
img.save("qrcode.png")You can generate QR codes directly from the command line using the qr script.
Standard PNG output:
qr "Some text" > test.pngUsing specific output flags (recommended for PowerShell):
qr --output=test.png "Some data"ASCII output:
qr --ascii "Some data" > "test.txt"SVG output with different factories:
qr --factory=svg-path "Some text" > test.svg
qr --factory=svg "Some text" > test.svg
qr --factory=svg-fragment "Some text" > test.svgqr "Some text" > test.png
qr --output=test.png "Some data"
qr --ascii "Some data" > "test.txt"
qr --factory=svg-path "Some text" > test.svgqrcode.image module provides access to different image factories used to render the QR Code (e.g., for PNG, SVG, or styled images).QRCode class. This allows you to explicitly add data and configure various parameters before generating the image via make_image().