Why a tiny QR generator script is worth five minutes
QR codes are a simple way to share URLs, dashboards or docs without typing long links. In this tutorial you will build a tiny Python script that turns any string into a QR code image and adds a centre logo in a single function call.
Just want a working implementation? Download the completed GitHub repo: python-qr-generator-tool on GitHub.
Just want a QR code without writing Python? Use the online tool: PLEX QR generator — paste a URL or text and download a PNG instantly.
Step 1: Install the QR code dependencies
We will use the qrcode library to generate the QR matrix and Pillow (PIL) to handle images and the logo overlay.
pip install "qrcode[pil]" Pillow
Make sure you are using a recent Python 3 environment (for example in a virtualenv) so this script stays isolated from your system packages.
Step 2: Create a minimal qrtool.py
Next, create a file called qrtool.py and paste in this minimal script. It focuses only on generating a QR code and placing a logo at the centre, without any extra styling.
#!/usr/bin/env python3
"""
Minimal QR generator with optional centre logo.
"""
from pathlib import Path
import qrcode
from PIL import Image
def make_qr_with_logo(data: str, logo_path: str | None, out_path: str = "qr.png") -> None:
# Use high error correction so the QR stays readable after adding a logo
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
# Base QR image
img = qr.make_image(fill_color="black", back_color="white").convert("RGBA")
if logo_path:
logo_file = Path(logo_path)
if not logo_file.is_file():
raise FileNotFoundError(f"Logo not found: {logo_file}")
logo = Image.open(logo_file).convert("RGBA")
# Keep logo around ~25% of QR width so it does not dominate the code
qr_w, qr_h = img.size
logo_size = int(qr_w * 0.25)
logo.thumbnail((logo_size, logo_size), Image.LANCZOS)
logo_w, logo_h = logo.size
pos = ((qr_w - logo_w) // 2, (qr_h - logo_h) // 2)
# Paste logo in the centre using alpha channel
img.alpha_composite(logo, dest=pos)
img.save(out_path)
print(f"Saved QR code to {out_path}")
if __name__ == "__main__":
# Simple example: change these values or wire up argparse if you prefer
make_qr_with_logo(
data="https://plexdata.online",
logo_path="plex_logo.png",
out_path="qr.png",
)
This keeps everything in one place: the QR code generation, safe error correction settings and a small logo overlay function that you can reuse in any project.
Step 3: Add a centre logo safely
When you put a logo in the middle of a QR code you cover some of the data modules. To keep the code scannable, the script does two things for you:
- Uses
ERROR_CORRECT_Hso the QR can tolerate more damage or obstruction. - Resizes the logo to roughly a quarter of the QR width so it stays visually strong without overwhelming the pattern.
All you need to supply is a square-ish logo file (for example plex_logo.png) in the same folder as the script.
Run through these checks whenever you generate a QR code with a logo:
- Scan the QR with at least two different apps or devices.
- Make sure the logo does not touch the three large corner squares (finder patterns).
- Keep strong contrast between the QR modules and the background.
- Export at a resolution that looks clean in your final layout (print or screen).
Step 4: Generate your own QR code
With qrtool.py saved and your logo file in place, you can generate a QR code in a single command:
python qrtool.py
Update the data, logo_path and out_path values in the if __name__ == "__main__" block to point at your own URL, logo file and output filename. You now have a reusable, minimal QR generator script you can drop into any project.
Skip the build: use the PLEX repo or web tool
If you would rather not type out the script yourself, you can clone the full version used in PLEX articles and demos:
Clone the complete tool: github.com/dorian-sotpyrc/python-qr-generator-tool
Or generate a QR code in your browser with no coding: https://plexdata.online/tools/qr-generator
Related PLEX reading
References & further reading
-
PyPI — qrcode
Official documentation for the Python qrcode library used to build the QR matrix. -
GeeksforGeeks — Generate QR Codes with a custom logo using Python
Example of combining qrcode and Pillow to insert a logo into the centre of a QR code. -
QRcode.com — Error correction feature
Official explanation of QR error correction levels and how much damage each level can tolerate. -
Scanova — QR Code Error Correction: How it works
Practical guidance on when to use higher error correction, especially when adding logos.