QR Code image generator with customizable image formats, error correction levels, and styling options
npx @tessl/cli install tessl/pypi-qrcode@7.4.0A comprehensive Python library for generating QR codes with support for multiple image formats, error correction levels, customizable styling, and both command-line and programmatic interfaces. Provides publication-quality QR code images through various backends including PIL, pure Python, and SVG.
pip install qrcode (basic) or pip install "qrcode[pil]" (with PIL support)import qrcodeFor specific functionality:
from qrcode import QRCode, make
from qrcode import constants
from qrcode import exceptionsimport qrcode
# Simple QR code generation
img = qrcode.make('Hello, World!')
img.save('qrcode.png')
# The image type depends on available libraries:
# - PIL available: returns qrcode.image.pil.PilImage
# - PIL not available: returns qrcode.image.pure.PyPNGImageimport qrcode
from qrcode import constants
# Create QRCode instance with custom settings
qr = qrcode.QRCode(
version=1, # Size (1-40, or None for auto)
error_correction=constants.ERROR_CORRECT_M, # Error correction level
box_size=10, # Pixels per box
border=4, # Border size in boxes
)
# Add data and generate
qr.add_data('Some data')
qr.make(fit=True) # Auto-size if version=None
# Create image with custom colors
img = qr.make_image(fill_color="black", back_color="white")
img.save('custom_qrcode.png')QRCode uses a layered architecture that separates QR code logic from image generation:
This design enables flexible output formats while maintaining QR code standard compliance.
Essential QR code creation functionality including the main QRCode class, quick generation function, version management, and data optimization features.
def make(data=None, **kwargs):
"""Quick QR code generation shortcut function."""
class QRCode:
"""Main QR code generator with full control over all parameters."""
def __init__(self, version=None, error_correction=constants.ERROR_CORRECT_M,
box_size=10, border=4, image_factory=None, mask_pattern=None): ...
def add_data(self, data, optimize=20): ...
def make(self, fit=True): ...
def make_image(self, image_factory=None, **kwargs): ...Multiple image output formats including PIL-based images, pure Python PNG, and various SVG formats. Each factory provides different capabilities and dependencies.
class PilImage:
"""PIL-based image factory supporting PNG, JPEG, and other PIL formats."""
class PyPNGImage:
"""Pure Python PNG image factory with no external dependencies."""
class SvgImage:
"""Standalone SVG image factory."""
class StyledPilImage:
"""Advanced PIL image factory with custom module drawers and color masks."""QR code error correction levels and configuration constants for controlling QR code generation parameters.
ERROR_CORRECT_L = 1 # ~7% error correction
ERROR_CORRECT_M = 0 # ~15% error correction (default)
ERROR_CORRECT_Q = 3 # ~25% error correction
ERROR_CORRECT_H = 2 # ~30% error correctionCommand-line interface and various output methods including ASCII terminal output, TTY color output, and file generation.
class QRCode:
def print_ascii(self, out=None, tty=False, invert=False): ...
def print_tty(self, out=None): ...
def get_matrix(self): ...Advanced styling capabilities including custom module shapes, color masks, embedded images, and various drawing patterns.
class StyledPilImage:
"""Styled PIL image with custom module drawers and color masks."""
class SquareModuleDrawer:
"""Square module drawer."""
class CircleModuleDrawer:
"""Circular module drawer."""
class QRColorMask:
"""Base class for color masking."""def run_example(data="http://www.lincolnloop.com", *args, **kwargs):
"""
Build and display an example QR Code for demonstration purposes.
Parameters:
- data (str): Data to encode (default: demo URL)
- *args, **kwargs: Arguments passed to QRCode constructor
Notes:
- Creates QRCode instance, adds data, generates image, and displays it
- Mainly for testing and demonstration purposes
"""class DataOverflowError(Exception):
"""Raised when data exceeds QR code capacity."""
class ActiveWithNeighbors:
"""
Named tuple containing module state with 3x3 neighborhood information.
Attributes:
- NW, N, NE: Northwest, North, Northeast neighbors (bool)
- W, me, E: West, current module, East (bool)
- SW, S, SE: Southwest, South, Southeast neighbors (bool)
Notes:
- Used by advanced module drawers for context-aware rendering
- Boolean evaluation returns the 'me' attribute value
"""
NW: bool
N: bool
NE: bool
W: bool
me: bool
E: bool
SW: bool
S: bool
SE: bool
# Type aliases for image factory parameters
from typing import Optional, Type, Union, Any, Dict, List
from qrcode.image.base import BaseImage
# Module matrix type
ModulesType = List[List[Optional[bool]]]