Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

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.

Medium Python 3.9+ Jun 27, 2026 Files & data 1 views 0 copies

Requires third-party packages — install first
pip install qrcode pillow

Python code

29 lines
Python 3.9+
import 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

stdout
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

  1. Use qrcode.make(data) for a one-liner QR generation without fine-grained version control.
  2. 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

Sponsored Reserved space — layout preview until AdSense is connected

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Files & data

Related tutorials and quizzes for this topic.