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

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (115) C (77) C# (12) C++ (82) Course (62) Coursera (178) coursewra (1) Cybersecurity (22) data management (11) Data Science (91) 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 (747) Python Coding Challenge (202) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

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