Friday, 22 March 2024

Creating QR Code with Logo

 

import qrcode

from PIL import Image


# Generate QR code for a URL

url = "https://www.clcoding.com"

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

qr.add_data(url)

qr.make(fit=True)


# Create an image with logo

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


# Add logo to the QR code

logo = Image.open("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

image.save("qr_code.png")

Image.open("qr_code.png")


Explantion of the code: 


This code generates a QR code for a given URL (https://www.clcoding.com) 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 "https://www.clcoding.com" 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 Image.open() 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 Image.open().
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.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (363) Python Quiz (25) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

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