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.

Continue Reading

Discover more amazing content handpicked just for you

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

Without this replacement, SQLAlchemy might throw an error like:

ValueError: Could not parse rfc1738 URL from string 'postgres://...'

Nov 08, 2024
Read More
Code
php

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

<?php
// Load WordPress environment
require_once('/path/to/your/wp-load.php');

// Get all posts (any post type)
$args = array(
    'post_type'      => 'any', // 'any' retrieves all post types (posts, pages, custom post types)
    'post_status'    => 'any', // 'any' retrieves all post statuses
    'posts_per_page' => -1,    // Retrieve all posts
);

$all_posts = get_posts($args);

foreach ($all_posts as $post) {
    // Get the post ID
    $post_id = $post->ID;

    // Check if the post has attachments (media files)
    $attachments = get_attached_media('', $post_id);

    // Delete each attachment associated with the post
    foreach ($attachments as $attachment) {
        $attachment_id = $attachment->ID;
        // This deletes the file from the uploads directory and the database record
        wp_delete_attachment($attachment_id, true);
    }

    // Delete the post itself
    wp_delete_post($post_id, true); // true = force delete, bypass trash
}

echo 'All posts and their uploads have been deleted.';
  • Backup: Make sure you have a backup of your database and uploads folder before running this script.
  • Use on a staging site: Test it on a staging environment before running on a live site, as it will permanently delete all posts and their uploads.

Oct 25, 2024
Read More
Code
php

How To enable all error debugging in PHP

No preview available for this content.

Oct 25, 2024
Read More
Code
bash

How to Paste in an SSH Terminal Without a Mouse

  • Copy: Ctrl + Shift + C to copy text.
  • Paste:
  • Ctrl + Shift + V
  • Alternatively: Cmd + V (macOS) or Shift + Insert (Linux).
  • Copy: Select text (it automatically copies to the clipboard).
  • Paste: Right-click or Shift + Insert.

Oct 20, 2024
Read More
Note
javascript nodejs

Vite vs Webpack

No preview available for this content.

Aug 14, 2024
Read More
Code
nodejs graphql

GraphQL API Server with Node.js and Apollo Server

First, create a new directory for your project and initialize it with npm:

   mkdir graphql-server
   cd graphql-server
   npm init -y

Aug 12, 2024
Read More
Code
javascript

React Custom Hook for API Requests

No preview available for this content.

Aug 12, 2024
Read More
Code
csharp

Unity Inventory System using Scriptable Objects

Create a scriptable object for defining item properties.

using UnityEngine;

// Define the base item as a scriptable object
[CreateAssetMenu(fileName = "NewItem", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
    public string itemName;
    public Sprite icon;
    public bool isStackable;
    public int maxStackSize = 1;

    public virtual void Use()
    {
        Debug.Log($"Using {itemName}");
    }
}

Aug 12, 2024
Read More
Code
csharp

Unity Player Controller Blueprint

No preview available for this content.

Aug 12, 2024
Read More
Code
bash

Bash: How to Loop Over Files in a Directory

No preview available for this content.

Aug 12, 2024
Read More
Code
csharp

C#: How to Read a File

No preview available for this content.

Aug 12, 2024
Read More
Code
html

HTML: Basic Template Structure

No preview available for this content.

Aug 12, 2024
Read More
Code
php

PHP: How to Connect to a MySQL Database

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

Aug 12, 2024
Read More
Code
css

CSS: How to Center a Div Horizontally and Vertically

No preview available for this content.

Aug 12, 2024
Read More
Code
java

Java: How to Sort a List

No preview available for this content.

Aug 12, 2024
Read More
Code
javascript nodejs

Node.js: How to Create an HTTP Server

No preview available for this content.

Aug 12, 2024
Read More
Code
sql

SQL: How to Select Top N Records

No preview available for this content.

Aug 12, 2024
Read More
Code
python

Python: How to Reverse a String

No preview available for this content.

Aug 12, 2024
Read More
Code
javascript json

How to Deep Clone a JavaScript Object

No preview available for this content.

Aug 12, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!