CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/pypi-qrcode

QR Code image generator with customizable image formats, error correction levels, and styling options

42%

Overall

Evaluation42%

0.86x

Agent success when using this tile

Overview
Eval results
Files

constants.mddocs/

Constants and Configuration

QR code error correction levels, encoding modes, and configuration constants that control QR code generation parameters and behavior.

Capabilities

Error Correction Constants

QR codes support four levels of error correction, allowing different amounts of the code to be damaged while still remaining readable.

from qrcode import constants

ERROR_CORRECT_L = 1    # Low error correction (~7%)
ERROR_CORRECT_M = 0    # Medium error correction (~15%, default)
ERROR_CORRECT_Q = 3    # Quartile error correction (~25%)
ERROR_CORRECT_H = 2    # High error correction (~30%)

Error Correction Level Details:

  • ERROR_CORRECT_L: ~7% of code words can be restored. Suitable for clean environments.
  • ERROR_CORRECT_M: ~15% of code words can be restored. Default and most common choice.
  • ERROR_CORRECT_Q: ~25% of code words can be restored. Good for environments with some damage risk.
  • ERROR_CORRECT_H: ~30% of code words can be restored. Best for harsh environments or when embedding images.

Usage Example:

import qrcode
from qrcode import constants

# Low error correction (smaller QR code)
qr_low = qrcode.QRCode(error_correction=constants.ERROR_CORRECT_L)

# High error correction (more robust, larger QR code)
qr_high = qrcode.QRCode(error_correction=constants.ERROR_CORRECT_H)

# Default (medium error correction)
qr_default = qrcode.QRCode()  # Uses ERROR_CORRECT_M by default

Encoding Mode Constants

Internal constants for QR code data encoding modes. These are typically used internally but may be relevant for advanced usage.

from qrcode import util

MODE_NUMBER = 1      # Numeric mode (0-9)
MODE_ALPHA_NUM = 2   # Alphanumeric mode (0-9, A-Z, space, $%*+-./:)
MODE_8BIT_BYTE = 4   # Byte mode (any 8-bit data)
MODE_KANJI = 8       # Kanji mode (Japanese characters)

Mode Characteristics:

  • MODE_NUMBER: Most efficient for numeric data (10 bits per 3 digits)
  • MODE_ALPHA_NUM: Efficient for uppercase alphanumeric data (11 bits per 2 characters)
  • MODE_8BIT_BYTE: Standard mode for any binary data (8 bits per byte)
  • MODE_KANJI: Efficient for Japanese Kanji characters (13 bits per character)

Usage Example:

import qrcode
from qrcode import util

# The library automatically selects optimal encoding modes
# But you can create custom QRData objects for specific modes

# Numeric data (automatically uses MODE_NUMBER)
qr = qrcode.QRCode()
qr.add_data('1234567890')

# Mixed data (automatically uses optimal combination)
qr = qrcode.QRCode()
qr.add_data('Order #123: $45.67')  # Uses different modes for different parts

# Force specific encoding (advanced usage)
numeric_data = util.QRData('1234567890', mode=util.MODE_NUMBER)
qr = qrcode.QRCode()
qr.add_data(numeric_data)

Version and Size Constants

QR code versions (sizes) range from 1 to 40, with each version having specific dimensions and data capacities.

# Version ranges
MIN_VERSION = 1      # Smallest QR code (21x21 modules)
MAX_VERSION = 40     # Largest QR code (177x177 modules)

# Version calculation:
# Size = (version * 4) + 17 modules per side
# Version 1: 21x21 modules
# Version 2: 25x25 modules
# ...
# Version 40: 177x177 modules

Version Selection:

import qrcode

# Auto-version (recommended)
qr = qrcode.QRCode(version=None)  # Will auto-size
qr.add_data('Some data')
qr.make(fit=True)  # Determines optimal version
print(f"Selected version: {qr.version}")

# Fixed version
qr = qrcode.QRCode(version=5)  # Always 37x37 modules
qr.add_data('Data')
qr.make()

# Check version capacity
try:
    qr = qrcode.QRCode(version=1)  # Very small
    qr.add_data('Too much data for version 1' * 100)
    qr.make()
except qrcode.exceptions.DataOverflowError:
    print("Data too large for specified version")

Configuration Parameters

Default and recommended values for QR code configuration parameters.

# Default QRCode parameters
DEFAULT_VERSION = None           # Auto-size
DEFAULT_ERROR_CORRECTION = 0     # ERROR_CORRECT_M
DEFAULT_BOX_SIZE = 10           # Pixels per module
DEFAULT_BORDER = 4              # Border thickness in modules (minimum per spec)

# Border constraints
MIN_BORDER = 4                  # Minimum border per QR code specification

Parameter Guidelines:

  • version: Use None for auto-sizing unless you need a specific size
  • error_correction: Use ERROR_CORRECT_M for general use, ERROR_CORRECT_H for embedded images
  • box_size: 10 pixels is good for most displays, adjust for print resolution
  • border: 4 is the minimum per specification, don't go below this

Usage Example:

import qrcode

# Recommended configuration for most uses
qr = qrcode.QRCode(
    version=None,                              # Auto-size
    error_correction=qrcode.constants.ERROR_CORRECT_M,  # Standard error correction
    box_size=10,                               # Standard pixel size
    border=4,                                  # Minimum border
)

# High-quality print configuration
qr_print = qrcode.QRCode(
    version=None,
    error_correction=qrcode.constants.ERROR_CORRECT_Q,  # Higher error correction
    box_size=20,                               # Larger pixels for print
    border=6,                                  # Extra border for safety
)

# Compact configuration (use with caution)
qr_compact = qrcode.QRCode(
    version=None,
    error_correction=qrcode.constants.ERROR_CORRECT_L,  # Lower error correction
    box_size=5,                                # Smaller pixels
    border=4,                                  # Minimum border
)

Mask Pattern Constants

QR codes use mask patterns to avoid problematic patterns in the final matrix. Usually auto-selected.

# Mask patterns (0-7)
MASK_PATTERN_AUTO = None    # Automatic selection (recommended)
MASK_PATTERNS = list(range(8))  # [0, 1, 2, 3, 4, 5, 6, 7]

Usage Example:

import qrcode

# Automatic mask selection (recommended)
qr = qrcode.QRCode(mask_pattern=None)

# Manual mask selection (advanced usage)
qr = qrcode.QRCode(mask_pattern=3)  # Use specific pattern

# Test different masks
for pattern in range(8):
    qr = qrcode.QRCode(mask_pattern=pattern)
    qr.add_data('Test data')
    qr.make()
    img = qr.make_image()
    img.save(f'qr_mask_{pattern}.png')

Configuration Best Practices

General Purpose QR Codes

qr = qrcode.QRCode(
    version=None,                              # Auto-size
    error_correction=qrcode.constants.ERROR_CORRECT_M,  # Standard
    box_size=10,                               # Standard
    border=4                                   # Minimum safe border
)

QR Codes with Embedded Images

qr = qrcode.QRCode(
    version=None,
    error_correction=qrcode.constants.ERROR_CORRECT_H,  # High for logo coverage
    box_size=10,
    border=4
)

High-Density Data

qr = qrcode.QRCode(
    version=None,                              # Let it auto-size large
    error_correction=qrcode.constants.ERROR_CORRECT_L,  # Lower for more data
    box_size=8,                                # Smaller for dense display
    border=4
)

Print Applications

qr = qrcode.QRCode(   
    version=None,
    error_correction=qrcode.constants.ERROR_CORRECT_Q,  # Higher for print quality
    box_size=20,                               # Larger for print resolution
    border=6                                   # Extra border for print tolerance
)

Install with Tessl CLI

npx tessl i tessl/pypi-qrcode@7.4.0
What are skills?

docs

console-interface.md

constants.md

core-generation.md

image-formats.md

index.md

styling.md

tile.json