Create QR code generator with logo
How to create QR code generator with logo with Python
Creating a QR code generator with logo in Python is best done using the qrcode
library, specifically its advanced image capabilities, combined with Pillow
(PIL) for image handling. This guide provides two methods: a simple, modern approach and a manual, detailed approach.
Installation
You'll need both the qrcode
library and Pillow for image manipulation. Install them together using the [pil]
extra:
pip install qrcode[pil]
Recommended method: the StyledPilImage
The most straightforward and modern way to embed a logo is by utilizing the qrcode
library's built-in image factories, which handle the resizing and safe positioning for you 3. This is the best practice as it reduces the chance of manual calculation errors.
Python code: StyledPilImage
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
# --- Configuration ---
DATA = "https://www.hrekov.com/blog"
LOGO_PATH = "logo.png" # Ensure this file exists
OUTPUT_FILE = "branded_qr_styled.png"
# --- Generation ---
# Initialize QR code with high error correction
# ERROR_CORRECT_H is crucial as it allows up to 30% of the code to be covered 5.
qr = qrcode.QRCode(
version=None, # Auto-select version based on data size
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
# Add data
qr.add_data(DATA)
qr.make(fit=True)
# Create the image using StyledPilImage
# Use the embedded_image_path argument to automatically insert the logo.
# Optional: Add styling like RoundedModuleDrawer for a modern look 3.
qr_img = qr.make_image(
image_factory=StyledPilImage,
embedded_image_path=LOGO_PATH,
module_drawer=RoundedModuleDrawer(), # Makes the little squares rounded
fill_color="darkgreen",
back_color="white"
)
# Save the final image
qr_img.save(OUTPUT_FILE)
print(f"Successfully generated styled QR code with logo: {OUTPUT_FILE}")
Manual method: PIL
for direct control
If you need finer control over the logo's size, placement, or want to apply custom image effects, you can manually resize and paste the logo onto the generated QR code using the Pillow library 1, 7.
Python code: manual Pillow
paste
import qrcode
from PIL import Image
# --- Configuration ---
DATA = "https://www.hrekov.com/blog"
LOGO_PATH = "logo.png"
OUTPUT_FILE = "branded_qr_manual.png"
# This base size determines the logo's max width inside the QR code
# 1/5th to 1/3rd of the total QR code size is a good safe range.
LOGO_MAX_WIDTH = 100
# --- QR Code Generation ---
# Initialize QR code with high error correction
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(DATA)
qr.make(fit=True)
# Create the base QR code image
qr_img = qr.make_image(
fill_color="black",
back_color="white"
).convert('RGB')
# --- Logo Processing ---
# Open the logo image
logo = Image.open(LOGO_PATH)
# Calculate new logo size while preserving the aspect ratio
# We scale the logo based on the maximum width defined above.
wpercent = (LOGO_MAX_WIDTH / float(logo.size[0]))
hsize = int((float(logo.size[1]) * float(wpercent)))
# Resize the logo
logo = logo.resize((LOGO_MAX_WIDTH, hsize), Image.Resampling.LANCZOS)
# Calculate the central position for the logo 1, 7.
qr_width, qr_height = qr_img.size
logo_width, logo_height = logo.size
# Calculate the top-left corner coordinates for centering
x_pos = (qr_width - logo_width) // 2
y_pos = (qr_height - logo_height) // 2
position = (x_pos, y_pos)
# Paste the logo onto the center of the QR code
qr_img.paste(logo, position)
# Save the final image
qr_img.save(OUTPUT_FILE)
print(f"Successfully generated manual QR code with logo: {OUTPUT_FILE}")
Best practices summary
- High error correction (H-level): Always use
qrcode.constants.ERROR_CORRECT_H
when adding a logo. This is mandatory for the QR code to remain scannable after part of it is covered 5. - Logo size: The logo should ideally not exceed 30% of the total QR code area to ensure readability. If the logo is too large, the code will be unscannable 1.
- Logo file format: Use a PNG image with a transparent background for the logo. This ensures only the logo itself is visible, not a surrounding white box.