Generate QR codes with a logo image embedded in the center. Use high error correction and paste a logo over the center 20% of the QR code, ensuring it remains scannable due to error correction redundancy.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Providing Python QR code snippet with centered logo
Here's a Python example using qrcode and Pillow that generates a QR code with high error correction and overlays a logo occupying 20% of the QR code's width at the center:
python
import qrcode
from PIL import Image
# Data to encode
data = "https://example.com"
# Create 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)
# Generate QR code image
img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
# Load logo and resize to 20% of the QR code width
logo = Image.open("logo.png")
img_w, img_h = img.size
logo_size = int(img_w * 0.2)
logo = logo.resize((logo_size, logo_size), Image.ANTIALIAS)
# Calculate position to paste the logo (centered)
pos = ((img_w - logo_size) // 2, (img_h - logo_size) // 2)
# Paste the logo onto the QR code with transparency mask if available
img.paste(logo, pos, mask=logo if logo.mode == 'RGBA' else None)
# Save the final QR code with logo
img.save("qr_with_logo.png")
Install dependencies with: pip install qrcode[pil] Pillow. Replace "logo.png" with your logo file path. This code ensures the QR code remains scannable due to high error correction.
Below is a simple Python example using the qrcode and Pillow libraries to generate a high-error-correction QR code, then overlay a logo at about 20% of the QR’s width/height so it remains scannable.
Installation:
bash
pip install qrcode[pil] pillow
Code (save as qr_with_logo.py for example):
python
import qrcode
from PIL import Image
# Text or URL you want to encode
data = "https://www.example.com"
# Create a QRCode object with high error correction
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
# Generate the QR code image
qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
# Load the logo image
logo = Image.open("logo.png")
# Compute logo size (20% of the QR code)
qr_width, qr_height = qr_img.size
logo_size = int(qr_width * 0.2) # 20% of QR code width
logo = logo.resize((logo_size, logo_size), Image.ANTIALIAS)
# Calculate position to place the logo at the center
pos_x = (qr_width - logo_size) // 2
pos_y = (qr_height - logo_size) // 2
# Paste the logo in the center (use logo as mask if it has transparency)
qr_img.paste(logo, (pos_x, pos_y), mask=logo if logo.mode == 'RGBA' else None)
# Save the final image
qr_img.save("qr_with_logo.png")
print("QR code saved as qr_with_logo.png")
How it works:
The QR code is generated with ERROR_CORRECT_H (high error correction) to allow up to ~30% of the code to be covered or damaged.
The script resizes your logo to about 20% of the QR’s width.
The logo is pasted into the center, and the error correction ensures the QR remains scannable.
Run the script, and you'll get qr_with_logo.png with your logo embedded in the center.