defgenerate_qr_with_logo(data, logo_path, output_path): """ Generates a QR code with a custom logo in the center.
Args: data: The data to encode in the QR code (string). logo_path: Path to the logo image (string). output_path: Path to save the generated QR code (string). """ try: # Create QR code instance qr = qrcode.QRCode( version=1, # Adjust version for more data error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction box_size=10, border=4, ) qr.add_data(data) qr.make(fit=True)
# Create an image from the QR code qr_image = qr.make_image(fill_color="black", back_color="white").convert("RGB")
# Open the logo image logo = Image.open(logo_path).convert("RGB")
# Calculate logo size (adjust as needed) logo_size = int(qr_image.size[0] * 0.2) # 20% of QR code size
# Resize the logo logo = logo.resize((logo_size, logo_size), Image.LANCZOS)
# Calculate logo position position = ((qr_image.size[0] - logo.size[0]) // 2, (qr_image.size[1] - logo.size[1]) // 2)
# Paste the logo onto the QR code qr_image.paste(logo, position)
# Save the QR code qr_image.save(output_path) print(f"QR code with logo saved to {output_path}")
except FileNotFoundError: print(f"Error: Logo file not found at {logo_path}") except Exception as e: print(f"An error occurred: {e}")