Of course! Adding a logo to a QR code is a great way to brand it. The key is to make sure the logo is small enough and has enough "quiet zone" (white space) around it so that QR code scanners can still read the data.

Here's a complete guide on how to do this in Python, from the basic installation to a full, reusable function.
Step 1: Install the Necessary Library
We'll use the qrcode library, which is the standard for generating QR codes in Python. You can install it using pip:
pip install qrcode[pil]
The [pil] part is important because it installs the Pillow library, which is needed to handle image manipulation (like adding your logo).
Step 2: The Basic Code (Without a Logo)
First, let's see how to generate a simple QR code. This is our foundation.

import qrcode
# The data you want to encode in the QR code
data = "https://www.python.org"
# Create a QRCode object
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
# Add the data to the QR code
qr.add_data(data)
qr.make(fit=True)
# Create an image from the QR code instance
img = qr.make_image(fill_color="black", back_color="white")
# Save the image to a file
img.save("basic_qr.png")
print("Basic QR code generated and saved as basic_qr.png")
Running this will create a file named basic_qr.png.
Step 3: Adding the Logo (The Core Logic)
To add a logo, we need to:
- Generate the QR code as an image.
- Open the logo image.
- Calculate the size of the logo. A good rule of thumb is to make it 1/5th the size of the QR code's overall dimensions.
- Calculate the position to center the logo on the QR code.
- Paste the logo onto the QR code image.
- Save the final image.
Here is the code to do that:
import qrcode
from PIL import Image
def add_logo_to_qr(qr_data, qr_path, logo_path, output_path):
"""
Generates a QR code and adds a logo to the center.
Args:
qr_data (str): The data to encode in the QR code.
qr_path (str): Path to save the generated QR code.
logo_path (str): Path to the logo image file.
output_path (str): Path to save the final QR code with the logo.
"""
# --- 1. Generate QR Code ---
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H, # Use high error correction
box_size=10,
border=4,
)
qr.add_data(qr_data)
qr.make(fit=True)
# Create an image from the QR code instance
qr_img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
# --- 2. Open Logo ---
try:
logo = Image.open(logo_path)
except FileNotFoundError:
print(f"Error: Logo file not found at {logo_path}")
return
# --- 3. Calculate Logo Size and Position ---
# Get the size of the QR code image
qr_width, qr_height = qr_img.size
# The logo should be roughly 1/5th the size of the QR code
logo_size = int(qr_width / 5)
# Resize the logo, using LANCZOS for high-quality downsampling
logo = logo.resize((logo_size, logo_size), Image.LANCZOS)
# Calculate the position to center the logo
logo_pos = ((qr_width - logo_size) // 2, (qr_height - logo_size) // 2)
# --- 4. Paste Logo onto QR Code ---
# Use the QR code image as a "mask" to ensure the logo is pasted correctly
qr_img.paste(logo, logo_pos)
# --- 5. Save the Final Image ---
qr_img.save(output_path)
print(f"QR code with logo saved successfully to {output_path}")
# --- Example Usage ---
if __name__ == "__main__":
# Prepare your files
qr_data = "https://github.com" # Your URL or text
qr_code_file = "qr_with_logo.png"
logo_file = "logo.png" # Make sure you have a logo.png in the same directory
# Call the function
add_logo_to_qr(qr_data, qr_code_file, logo_file, qr_code_file)
Before you run this:
- Save the code above as a Python file (e.g.,
qr_generator.py). - Get a logo image (e.g.,
logo.png) and place it in the same directory as your script. For best results, use a PNG with a transparent background.
Important Considerations for Success
-
Error Correction is Crucial: When you add a logo, you are covering up parts of the QR code. This can make it unreadable. To prevent this, use a high error correction level. In the code, we use
ERROR_CORRECT_H(High), which can recover up to 30% of the code. This is highly recommended. -
Logo Size: The 1/5th rule is a good starting point. If your logo is too large, it will destroy the QR code pattern. If it's too small, it might not be visible. Experiment to find the right balance.
-
Logo Format: A PNG with a transparent background works best. This way, the white (or colored) square of the QR code behind the logo won't interfere with your branding.
-
Quiet Zone: The
borderparameter in the QRCode object creates a "quiet zone"—a margin of empty space around the QR code. This is essential for scanners. A border of 4 is the minimum recommended value. If your logo is too close to the edge, it can merge with this border and cause scanning errors.
Full Reusable Function with Error Handling
Here is a more robust version of the function that includes better error handling and comments.
import qrcode
from PIL import Image
def create_qr_with_logo(
data: str,
logo_path: str,
output_path: str = "qr_logo.png",
box_size: int = 10,
border: int = 4,
):
"""
Generates a QR code with a centered logo and saves it.
Args:
data (str): The data to encode in the QR code.
logo_path (str): Path to the logo image file (PNG with transparency recommended).
output_path (str): Path to save the final QR code image.
box_size (int): The number of pixels for each "box" in the QR code.
border (int): The number of boxes for the white border around the QR code.
"""
print("Starting QR code generation...")
# 1. Generate QR Code with High Error Correction
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction
box_size=box_size,
border=border,
)
qr.add_data(data)
qr.make(fit=True)
# Create an image from the QR code instance
qr_img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
# 2. Open and Prepare the Logo
try:
logo = Image.open(logo_path)
except FileNotFoundError:
print(f"Error: Logo file not found at '{logo_path}'. Please check the path.")
return
except Exception as e:
print(f"An error occurred while opening the logo: {e}")
return
# 3. Resize the Logo
# The logo should be roughly 1/5th the size of the QR code's data area
qr_width, qr_height = qr_img.size
logo_size = int(qr_width / 5)
# Ensure the logo is not larger than the QR code's data area
if logo_size > qr_width - 2 * border * box_size:
print("Warning: Logo is too large for the QR code. Reducing size.")
logo_size = qr_width - 2 * border * box_size - 10 # Add some padding
logo = logo.resize((logo_size, logo_size), Image.LANCZOS)
# 4. Calculate Position and Paste
logo_pos = ((qr_width - logo_size) // 2, (qr_height - logo_size) // 2)
qr_img.paste(logo, logo_pos, logo) # Use the logo as its own mask for transparency
# 5. Save the Final Image
try:
qr_img.save(output_path)
print(f"Success! QR code with logo saved to '{output_path}'")
except Exception as e:
print(f"An error occurred while saving the image: {e}")
# --- Example Usage ---
if __name__ == "__main__":
# Make sure you have 'my_logo.png' in the same folder
# You can download a sample logo or create your own.
# Example 1: Simple website URL
create_qr_with_logo(
data="https://www.python.org",
logo_path="my_logo.png",
output_path="python_website_qr.png"
)
# Example 2: Wi-Fi credentials (use with caution, format may vary)
# wifi_data = "WIFI:T:WPA;S:MyNetwork;P:MyPassword;;"
# create_qr_with_logo(
# data=wifi_data,
# logo_path="my_logo.png",
# output_path="wifi_qr.png"
# )
This final script is a great starting point for any project that requires branded QR codes. Just remember to use a transparent logo and ensure the error correction is set high enough (ERROR_CORRECT_H).
