About Project :
In this article, we will discuss how to generate a QRcode using python.
QR code (Quick Response code) is a type of the matrix barcode invented in 1994 by the Japanese automotive company Denso Wave. A barcode is a machine-readable optical label that can contain information about the item to which it is attached.
Using Python packages like qrcode, pyqrcode modules we can generate and read the QR codes. In this project, we will see how we can generate the QR Codes using python.
In this project, we will take the link from the user and the basic requirements like name, box size, etc, and generate a QRcode with the given information.
You can generate the QR code and save it in png format.
Project Prerequisites:
- You have to install the pip packages known as qrcode and image.
- qrcode: the python package used to generate the QRcode by taking user requirements.
- Image: this package is used to save the generated QR Code in our required image format.
Getting Started:
Let’s Get started…
Hello World. Here, I will show you how to create a QRcode using Python.
Let’s see how to generate QRcode in Python.
First, we need to install 2 modules using the cmd
qrcode (pip install QRcode)
image (pip install image)
The module automates most of the building process for creating QR codes. This module attempts to follow the QR code standard as closely as possible.
>pip install image
>pip install qrcode
Now, let’s move on to the coding part…
qr = qrcode.QRCode(version ,box_size,border )
When creating a QR code only the content to be encoded is required, all the other properties of the code will be guessed based on the contents given. This function will return a QR object.
Here, is the version: This parameter specifies the size and data capacity of the code.
box_size: the size of the box where the QR code will be displayed.
border: it is the size of the border around the QRCode usually white in color.
Program:
#Encoding Data into Quick Response Code (QR Code)
import qrcode
# Data to encode
data = input('Enter the Data: ')
version=int(input('Enter the version (complexity): ')) #maxvalue 15
box_size=int(input('Enter the Box size: ')) # max value 10
# Creating an instance of QRCode class
qr = qrcode.QRCode(version ,box_size,border = 5)
# Adding data to the instance 'qr'
qr.add_data(data)
qr.make(fit = True)
img = qr.make_image(fill_color = 'black',back_color = 'white')
f=input("name it as: ") #image name
img.save(f'{f}.png')
print('qr code generated and saved in the gallery')
after the execution of the program, the QR code will be generated in the same directory with the user’s given name in .png format.
Example:
You can run the above code like this:
Output:
Congratulations, Now you have generated a QR code using python.
You can scan it. It will direct you to youtube.com.
Sir I want to learn this: But I am not finding it easy
It’s very easy dear…Just learn python Basics from any platform(incase if you don’t know already) and start understanding the code and start implementing it.
Use of special words like “version” and “box_size” should be avoided as variables.