Friday 22 March 2024

Creating QR Code with Logo


import qrcode

from PIL import Image

# Generate QR code for a URL

url = ""

qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=8, border=5)



# Create an image with logo

image = qr.make_image(fill_color="black", back_color="pink")

# Add logo to the QR code

logo ="clcodinglogo.png")

logo_size = img.size[0] // 4

# Use Image.LANCZOS for resizing with anti-aliasing

logo = logo.resize((logo_size, logo_size), Image.LANCZOS)  

image.paste(logo, ((img.size[0] - logo.size[0]) // 2, (img.size[1] - logo.size[1]) // 2))

# Save the image"qr_code.png")"qr_code.png")

Explantion of the code: 

This code generates a QR code for a given URL ( using the qrcode library and then adds a logo to the QR code using the PIL (Python Imaging Library) module. Let's break down the code step by step:

Importing libraries:

qrcode: This library is used to generate QR codes.
Image from PIL: This module provides functions to work with images.
Generating the QR code:

The URL "" is assigned to the variable url.
A QRCode object is created with specific parameters:
version: The version of the QR code (higher versions can store more data).
error_correction: The error correction level of the QR code.
box_size: The size of each box in the QR code.
border: The width of the border around the QR code.
The URL data is added to the QR code using the add_data() method.
The make() method is called to generate the QR code, and fit=True ensures that the size of the QR code fits the data.
Creating an image with the QR code:

The make_image() method is called to create an image representation of the QR code, with specified fill and background colors (fill_color="black", back_color="pink").
The resulting image is stored in the variable image.
Adding a logo to the QR code:

An image of the logo (clcodinglogo.png) is opened using the method and stored in the variable logo.
The size of the logo is calculated to be a quarter of the size of the QR code.
The logo is resized using the resize() method with anti-aliasing (Image.LANCZOS filter) to prevent distortion.
The logo is pasted onto the QR code image at the center using the paste() method.
Saving and displaying the final image:

The QR code image with the logo is saved as "qr_code.png" using the save() method.
The saved image is opened and displayed using
This code demonstrates how to generate a customized QR code with a logo using Python. Make sure to replace "clcodinglogo.png" with the filename of your logo image.


Post a Comment

Popular Posts


AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (87) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (740) Python Coding Challenge (188) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)


Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses