QR Code Generation¶
🎯 What the Package Does¶
The qr_generator package creates PNG QR‑code images that encode a mixtape’s public URL.
Two flavours are offered:
| Function | Output | Extra visual elements |
|---|---|---|
generate_mixtape_qr |
Plain QR code (square) | Optional centred logo (SVG/PNG) |
generate_mixtape_qr_with_cover |
Composite image (cover → title banner → QR) | Optional cover art, title banner, centred logo |
Both functions return raw bytes (PNG data) ready to be sent as a Flask Response or saved to disk.
🔧 Installation & Dependencies¶
| Setting | Where it lives | Default | Remarks |
|---|---|---|---|
qrcode (Python library) |
project.toml / uv.lock |
>=7.4 |
Required for both endpoints. |
Pillow (image handling) |
project.toml |
>=10.0 |
Needed for compositing the logo/cover. |
| Static logo files | static/images/logo.svg or static/logo.png |
— | The blueprint prefers SVG; falls back to PNG. |
| Cover directory | app.config["COVER_DIR"] (set in config.py) |
collection-data/mixtapes/covers |
Used only by the download endpoint. |
| Cache-Control | Hard-coded in the view (public, max-age=3600). |
— | Adjust in the source if you need a different TTL. |
⚠️ Error Handling¶
| Situation | Raised Exception | Message (visible to caller) |
|---|---|---|
qrcode library missing |
ImportError (raised at the top of each public function) |
"qrcode library not installed. Install with: uv add qrcode pillow" |
| Logo or cover path does not exist | Silently ignored — the function falls back to a plain QR or a QR without cover. Errors are printed to stdout (print) but not propagated. |
— |
Pillow fails to open an image (corrupt file) |
Caught inside the helper; continues processing | Prints "Failed to load cover: …" or "Failed to add logo to QR code: …" and continues with the rest of the image. |
Invalid size / qr_size (negative, zero) |
Not explicitly validated — Pillow raises a ValueError during resize |
Exception propagates to the caller (treated as a 500 error in the Flask view). |
Best practice – Validate user‑supplied sizes before calling the functions, or wrap the call in a try/except block and return a friendly error page.
🔌 API¶
qr_generator
¶
Enhanced QR Code generation with mixtape cover art and title.
Functions:
| Name | Description |
|---|---|
generate_mixtape_qr_with_cover |
Generate a branded QR code with mixtape cover art and title. |
generate_mixtape_qr |
Generate a simple QR code (backward compatibility). |
generate_mixtape_qr_with_cover(url, title, cover_path=None, logo_path=None, qr_size=400, include_title=True)
¶
Generate a branded QR code with mixtape cover art and title.
Creates a composite image with: - Mixtape cover art at the top - Title banner below cover - QR code at the bottom - Optional logo in QR center
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The full URL to encode in the QR code |
required |
title
|
str
|
The mixtape title to display |
required |
cover_path
|
Path | None
|
Path to mixtape cover image |
None
|
logo_path
|
Path | None
|
Optional path to logo for QR center |
None
|
qr_size
|
int
|
Size of the QR code portion (default 400) |
400
|
include_title
|
bool
|
Whether to include title banner (default True) |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
PNG image data of the complete branded QR code |
Raises:
| Type | Description |
|---|---|
ImportError
|
If qrcode library is not installed |
Source code in src/qr_generator/qr_generator.py
generate_mixtape_qr(url, title, logo_path=None, size=400)
¶
Generate a simple QR code (backward compatibility).
This is the original function for basic QR generation.
