How to Generate Beautiful QR Codes with Embedded Logos in Python
Generate a high-error-correction QR code and paste a logo image in the center to create a branded, scannable QR code.
pip install qrcode pillow
Python code
29 linesimport qrcode
from PIL import Image
def generate_qr_with_logo(data, logo_path, output_path):
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)
qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
logo = Image.open(logo_path).resize((60, 60), Image.Resampling.LANCZOS)
pos = ((qr_img.size[0] - logo.size[0]) // 2,
(qr_img.size[1] - logo.size[1]) // 2)
qr_img.paste(logo, pos, mask=logo if logo.mode == 'RGBA' else None)
qr_img.save(output_path)
print(f"QR code saved to {output_path}")
if __name__ == "__main__":
# Replace with your own logo image path and desired output
generate_qr_with_logo(
data="https://example.com",
logo_path="example_logo.png", # Must be an existing image file
output_path="beautiful_qr.png"
)
Output
QR code saved to beautiful_qr.png
How it works
The qrcode library generates QR code images; Pillow (PIL) handles image loading and manipulation. ERROR_CORRECT_H (≈30% error correction) is essential because embedding a logo blocks central modules – without it, scanning the QR code would often fail. The code resizes the logo to 60×60 pixels using high-quality Lanczos resampling, then pastes it at the exact center of the QR code. When the logo has an alpha channel (RGBA mode), the mask parameter preserves transparency; otherwise the logo pastes as a solid rectangle.
Common mistakes
- Using low error correction (e.g., ERROR_CORRECT_L) – the QR code becomes unscannable after inserting the logo.
- Pasting a logo that is too large or too small – the recommended logo size is 15–20% of the QR code side length.
- Forgetting to install both qrcode and Pillow (PIL) – the code will raise an ImportError.
- Not handling missing image files – the code will crash with FileNotFoundError if logo_path doesn't exist.
Variations
- Use qrcode.make(data) for a one-liner QR generation without fine-grained version control.
- Add rounded corners to the logo before pasting with PIL's ImageDraw or a mask.
Real-world use cases
- Marketing materials like flyers and business cards where a branded logo sits inside the QR code.
- Product packaging QR codes that link to manuals or promotional pages while retaining brand identity.
- Digital tickets or event badges that need a scannable QR code combined with a sponsor logo.
Sponsored
More from Files & data
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a Python Script That Detects and Deletes Empty Files Across Folders easy
- Compare Two Folder Structures and Find Differences in Python easy
- Compress and Extract ZIP Files Programmatically in Python easy
- Convert CSV Files to JSON in Python easy
- Convert Image to ASCII Art in Python medium
Keep learning
Related tutorials and quizzes for this topic.