$ show lincolnloop/python-qrcode
This is a mature, well-maintained pure Python library for generating QR codes. It provides a core QR code encoder that implements the full ISO 18004 standard, including error correction, masking, and version selection. The library supports multiple output formats via a pluggable image factory architecture: PIL (default), PyPNG, and several SVG variants (basic, fragment, path). It also includes a styled PIL image factory that allows custom module shapes (circles, rounded squares, bars) and color masks (gradients, image fills, solid colors) for visually customized QR codes. The codebase is well-structured with clear separation of concerns: QR code encoding logic in `util.py` and `base.py`, image output in `qrcode/image/`, styling in `qrcode/image/styles/`, and a CLI entry point via `console_scripts.py`. It has extensive test coverage, uses modern Python tooling (uv, ruff, GitHub Actions), and follows semantic versioning. The architecture is extensible: new image factories or module drawers can be added by subclassing base classes. The code is clean, well-commented, and demonstrates high craftsmanship in areas like bit manipulation, Reed-Solomon error correction, and efficient SVG path generation.
The LLM identified 6 files worth highlighting for reuse:
The QRCode class orchestrates the entire QR code generation process. It manages data input, version selection (best fit), mask pattern optimization, module placement, and image generation. The `best_mask_pattern()` method evaluates all 8 mask patterns using the full symbol (including format/version info) per ISO 18004 ยง7.8.3.1, selecting the one with the lowest penalty score. The `make_image()` method uses the image factory pattern and supports embedded images. The class is self-contained and could be extracted with minimal changes to serve as a standalone QR code encoder.
This module handles all data encoding aspects: mode selection (numeric, alphanumeric, byte), optimal data chunking via `optimal_data_chunks()`, bit buffer management, and Reed-Solomon error correction code generation. The `optimal_data_chunks` function uses regex patterns to split data into the most efficient encoding modes, significantly reducing QR code size. The `create_data` function builds the final data stream with proper termination, padding, and interleaving. The precomputed `BIT_LIMIT_TABLE` and `rsPoly_LUT` (in LUT.py) optimize performance. This module is highly reusable for any project needing QR code encoding logic.
Defines the abstract `BaseImage` class and `BaseImageWithDrawer` mixin that form the foundation for all image output factories. The `BaseImage` provides a template method for rendering: `drawrect` is called for each active module, with optional `drawrect_context` for neighbor-aware drawing. `BaseImageWithDrawer` adds a strategy pattern for module drawing via `QRModuleDrawer` subclasses, with separate drawers for the finder eyes. This design makes it easy to add new output formats (e.g., PDF, matplotlib) by subclassing `BaseImage` and implementing `drawrect`, `new_image`, and `save`.
The `StyledPilImage` factory extends `BaseImageWithDrawer` to provide highly customizable QR code images. It uses a `QRColorMask` strategy to recolor the foreground pixels (supports gradients, image fills, solid colors) and a `QRModuleDrawer` strategy to shape individual modules (circles, rounded squares, bars). It also supports embedding an image in the center of the QR code. The color mask algorithm preserves antialiasing artifacts by interpolating between foreground and background colors. This component is a good example of the strategy pattern for extensible image styling.
Contains the PIL implementations of module drawers: `SquareModuleDrawer`, `GappedSquareModuleDrawer`, `CircleModuleDrawer`, `GappedCircleModuleDrawer`, `RoundedModuleDrawer`, `VerticalBarsDrawer`, and `HorizontalBarsDrawer`. These drawers use a technique of rendering at 4x resolution (ANTIALIASING_FACTOR) then downscaling for smooth antialiased edges. The `RoundedModuleDrawer` and bar drawers are neighbor-aware (`needs_neighbors = True`), meaning they consider the active state of adjacent modules to draw rounded corners only where appropriate. This pattern is reusable for any project needing custom shapes on a grid.
Provides multiple SVG image factories: `SvgFragmentImage`, `SvgImage`, `SvgPathImage`, and their fill-background variants. `SvgPathImage` is particularly notable as it combines all QR code modules into a single SVG `<path>` element, reducing file size by over 30% compared to the `<rect>`-based approach. The factories use the `SvgFragmentImage` base which generates SVG fragments without the XML declaration, useful for embedding in HTML. The `units` method converts pixel coordinates to millimeters, maintaining physical size. The SVG drawer aliases (circle, gapped-square, etc.) are defined at the factory level, making the CLI `--factory-drawer` option work seamlessly. This component is a clean example of generating vector graphics from a grid of modules.