DeveloperBreeze

QR Code with Embedded Logo

python
import qrcode
from PIL import Image

def create_qr_with_logo(url, logo_path, output_path):
    # Generate a basic QR code
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=4,
    )
    qr.add_data(url)
    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')

    # Open the logo image
    logo = Image.open(logo_path)

    # Calculate the size of the logo to be embedded
    qr_width, qr_height = qr_img.size
    logo_size = (qr_width // 4, qr_height // 4)
    logo = logo.resize(logo_size, Image.ANTIALIAS)

    # Calculate position to place the logo
    logo_position = (
        (qr_width - logo_size[0]) // 2,
        (qr_height - logo_size[1]) // 2
    )

    # Paste the logo image onto the QR code
    qr_img.paste(logo, logo_position, mask=logo)

    # Save the resulting QR code to a file
    qr_img.save(output_path)

# Example usage
create_qr_with_logo(
    url="https://www.example.com",
    logo_path="logo.png",  # Path to your logo image
    output_path="qr_with_logo.png"
)

How It Works

  1. Dependencies: This code requires the qrcode and Pillow libraries. You can install them via pip:
   pip install qrcode[pil]
  1. QR Code Generation: The code creates a QR code using the qrcode library. It sets a high error correction level (ERROR_CORRECT_H) to allow for the logo to be embedded.
  2. Logo Embedding: It opens the logo image using Pillow, resizes it, and calculates the position to place it at the center of the QR code.
  3. Image Pasting: The logo is pasted onto the QR code using the paste method with a mask to maintain transparency.
  4. Saving the Image: The final QR code with the embedded logo is saved as a PNG file.

Use Cases

  • Custom QR Codes: Use this script to generate QR codes with a personalized touch by embedding your brand's logo.
  • Marketing: Create QR codes that are visually appealing and easily recognizable for marketing campaigns.
  • Educational: Demonstrate the use of Python libraries for image processing and QR code generation.

Related Posts

More content you might like

Code

How to Create a New MySQL User with Full Privileges

No preview available for this content.

May 01, 2025
Read More
Code
python

Configuring SQLAlchemy with PostgreSQL on Heroku: A Quick Guide

  • When you provision a PostgreSQL database on Heroku, it sets an environment variable called DATABASE_URL with the connection string for your database.
  • This URI uses the format postgres://.
  • SQLAlchemy (especially when using the psycopg2 driver for PostgreSQL) requires the URI to be in the format postgresql+psycopg2://.
  • This prefix specifies the dialect (postgresql) and the driver (psycopg2) explicitly, which SQLAlchemy uses to establish the connection.

Nov 08, 2024
Read More
Code
php

How to Delete All WordPress Posts and Their Uploads Using a Custom PHP Script

To clear all posts and their associated uploads in WordPress from your custom PHP script using wp-load.php, you can follow these steps:

Make sure your script has access to the WordPress environment by loading wp-load.php:

Oct 25, 2024
Read More
Code
php

How To enable all error debugging in PHP

  • error_reporting(E_ALL);: Enables reporting for all levels of errors.
  • ini_set('display_errors', 1);: Ensures that errors are displayed in the browser.
  • If you prefer to log the errors instead of displaying them, you can uncomment the log_errors and error_log lines, specifying the path where errors should be logged.

Oct 25, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!