QR Code image generator with customizable image formats, error correction levels, and styling options
Multiple image output formats for QR codes, each with different capabilities, dependencies, and use cases. The qrcode library supports PIL-based images, pure Python PNG, various SVG formats, and advanced styled images.
The PIL image factory provides the most flexible image output with support for multiple formats and color modes.
from qrcode.image.pil import PilImage
class PilImage:
"""
PIL-based image factory supporting multiple formats.
Requirements:
- pillow package
Supported formats:
- PNG (default)
- JPEG
- BMP
- GIF
- Any PIL-supported format
"""
kind = "PNG" # Default format
def save(self, stream, format=None, **kwargs):
"""
Save image to stream or file.
Parameters:
- stream: File path (str) or file-like object
- format (str or None): Image format override
- **kwargs: Additional PIL save parameters
"""Usage Example:
import qrcode
from qrcode.image.pil import PilImage
# Use PIL factory explicitly
qr = qrcode.QRCode(image_factory=PilImage)
qr.add_data('Hello')
qr.make()
# Create image with custom colors
img = qr.make_image(fill_color="darkblue", back_color="lightgray")
img.save('colored_qr.png')
# Save as JPEG
img.save('qr_code.jpg', format='JPEG', quality=95)Pure Python PNG image factory with no external dependencies beyond the built-in pypng library.
from qrcode.image.pure import PyPNGImage
class PyPNGImage:
"""
Pure Python PNG image factory.
Requirements:
- pypng package (included with qrcode)
Features:
- No PIL dependency
- PNG output only
- Grayscale 1-bit output
- Compact file sizes
"""
kind = "PNG"
allowed_kinds = ("PNG",)
def save(self, stream, kind=None):
"""
Save PNG image to stream or file.
Parameters:
- stream: File path (str) or file-like object
- kind: Must be "PNG" or None
"""Usage Example:
import qrcode
from qrcode.image.pure import PyPNGImage
# Use pure Python PNG factory
qr = qrcode.QRCode(image_factory=PyPNGImage)
qr.add_data('Hello')
qr.make()
img = qr.make_image()
img.save('pure_python_qr.png')
# Backward compatibility alias (deprecated)
from qrcode.image.pure import PymagingImageMultiple SVG image factories providing different SVG output styles and features.
from qrcode.image.svg import SvgImage, SvgFragmentImage, SvgPathImage, SvgFillImage, SvgPathFillImage
class SvgImage:
"""
Standalone SVG image with full XML declaration.
Features:
- Complete SVG document
- Scalable vector output
- Custom drawer support
- XML declaration included
"""
kind = "SVG"
drawer_aliases = {
"circle": ("SvgCircleDrawer", {}),
"gapped-circle": ("SvgCircleDrawer", {"size_ratio": 0.8}),
"gapped-square": ("SvgSquareDrawer", {"size_ratio": 0.8}),
}
class SvgFragmentImage:
"""
SVG fragment without XML declaration.
Features:
- SVG fragment for embedding
- No XML declaration
- Smaller output
"""
class SvgPathImage:
"""
SVG with single path element for compact output.
Features:
- Single <path> element
- Most compact SVG output
- No gaps between modules
"""
class SvgFillImage:
"""SVG image with white background fill."""
background = "white"
class SvgPathFillImage:
"""SVG path image with white background fill."""
background = "white"Usage Example:
import qrcode
from qrcode.image.svg import SvgImage, SvgPathImage, SvgFragmentImage
# Standard SVG
qr = qrcode.QRCode(image_factory=SvgImage)
qr.add_data('Hello')
qr.make()
img = qr.make_image()
img.save('standard.svg')
# Path-based SVG (most compact)
qr = qrcode.QRCode(image_factory=SvgPathImage)
qr.add_data('Hello')
qr.make()
img = qr.make_image()
img.save('path.svg')
# SVG fragment for embedding
qr = qrcode.QRCode(image_factory=SvgFragmentImage)
qr.add_data('Hello')
qr.make()
fragment = qr.make_image()
svg_content = fragment.to_string()Advanced PIL image factory with support for custom module shapes, color masks, and embedded images.
from qrcode.image.styledpil import StyledPilImage
class StyledPilImage:
"""
Advanced styled PIL image factory.
Features:
- Custom module drawers
- Color masks and gradients
- Embedded images/logos
- Antialiasing support
"""
kind = "PNG"
needs_processing = True
def __init__(self, *args, module_drawer=None, color_mask=None,
embeded_image=None, embeded_image_path=None,
embeded_image_resample=None, **kwargs):
"""
Initialize styled PIL image.
Parameters:
- module_drawer: Custom module drawer instance
- color_mask: Color mask for advanced coloring
- embeded_image: PIL Image to embed in center
- embeded_image_path (str): Path to image file to embed
- embeded_image_resample: Resampling filter for embedded image
"""Usage Example:
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers import CircleModuleDrawer
from qrcode.image.styles.colormasks import SolidFillColorMask
# Create styled QR code with circular modules
qr = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction for embedded image
image_factory=StyledPilImage
)
qr.add_data('Hello Styled World')
qr.make()
img = qr.make_image(
module_drawer=CircleModuleDrawer(),
color_mask=SolidFillColorMask(back_color=(255, 255, 255), front_color=(0, 0, 100)),
embeded_image_path='logo.png'
)
img.save('styled_qr.png')The qrcode library automatically selects the best available image factory based on installed dependencies.
# Automatic factory selection priority:
# 1. PilImage (if pillow is installed)
# 2. PyPNGImage (fallback, always available)
# In make() function and QRCode.make_image():
def make_image(self, image_factory=None, **kwargs):
"""
Factory selection logic:
1. Use provided image_factory parameter
2. Use QRCode.image_factory if set
3. Auto-detect: PilImage if available, else PyPNGImage
"""Usage Example:
import qrcode
# Automatic selection
img = qrcode.make('Hello')
print(type(img)) # Shows selected factory type
# Manual selection
from qrcode.image.pure import PyPNGImage
img = qrcode.make('Hello', image_factory=PyPNGImage)
# Per-instance factory
qr = qrcode.QRCode(image_factory=PyPNGImage)
qr.add_data('Hello')
qr.make()
img = qr.make_image() # Uses PyPNGImageDifferent image factories support different output formats and saving options.
# Common saving patterns across factories:
# File path saving
img.save('qrcode.png')
img.save('qrcode.jpg', format='JPEG')
# Stream saving
with open('qrcode.png', 'wb') as f:
img.save(f)
# Format-specific options
img.save('qrcode.jpg', format='JPEG', quality=95, optimize=True)
img.save('qrcode.png', format='PNG', compress_level=9)SVG-specific methods:
# SVG images have additional methods
svg_img = qr.make_image() # Using SVG factory
# Get SVG as string
svg_string = svg_img.to_string()
# Save with encoding options
svg_img.save('qrcode.svg') # Full XML document| Factory | Dependencies | Formats | File Size | Features |
|---|---|---|---|---|
| PilImage | pillow | PNG, JPEG, BMP, GIF, etc. | Medium | Full color support, many formats |
| PyPNGImage | pypng (built-in) | PNG only | Small | No external deps, compact |
| SvgImage | None | SVG | Smallest | Scalable, text-based |
| SvgPathImage | None | SVG | Smallest | Most compact SVG |
| StyledPilImage | pillow | PNG, JPEG, etc. | Largest | Advanced styling, logos |
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10