Create a Quick Response (QR) Code Using Python
Understanding the QR Code
- QR code is a machine-readable barcode designed in two-dimensional pixelated form.
- The QR code can be used to store a range of data.
- QR in QR code is abbreviated for Quick Response.
- QR code was invented in the year 1994 by Masahiro Hara, a Japanese engineer from Automobile Manufacturer Denso Wave, in order to track the movement of car parts.
- The popularity of the QR code has increased in the later 2010s with improvement in optical proficiencies of Mobile Phones and their extensive acceptance.
- At the moment, QR codes are being utilized for a wide range of applications such as making online payments, checking hotel menus, sharing Wi-Fi passwords, obtaining cost and other information of products, and a lot more.
- QR codes have become so famous that now every new smartphone comes with a built-in QR code reader.
Using the computer language Python, we are going to go through the steps of creating and reading a QR code in the following lesson.
Ok, let’s get
started.
Generating QR Code using Python
It is possible for us to create a QR code by using the computer language Python, which comes with a variety of different modules and packages. In order to produce the code needed for this course, we will be use the qrcode package as our primary tool.
Installing everything first, however, is necessary before we can begin using the software.
it.
Installing the Python qrcode package
With the following command, which requires the assistance of the pip installer, we can successfully install the qrcode package: Syntax:
-
$ pip install qrcode
The version of Python and the package will be installed on the system at the same time.
pip.
Verifying the Installation
We may attempt importing the package and running the software in order to determine whether or not the package was correctly installed in the system. This will allow us to determine whether or not the installation was successful.
When the installation is finished, you will need to create a new Python file and include the following syntax inside it. Example:
-
# importing the required module
-
import
qrcode
Now, save the file, and then use the command prompt to execute the saved file by entering the following command. Syntax:
-
$ python <file-name>.py
If you execute the application and it does not throw any import errors, then the module has been installed correctly. In any other case, it is strongly suggested that the package be reinstalled, and the official documentation should be consulted.
Now that we have it sorted out, let’s get to work with the QR code.
library.
Generating a Simple QR Code
By using the make function of the qrcode library and passing the data in as the function’s input, we can easily construct a basic QR code.
Have a look at the following example, which generates a QR code that says “Welcome to Javatpoint.” Example:
-
# importing the qrcode library
-
import
qrcode
-
# generating a QR code using the make() function
-
qr_img = qrcode.make(
“Welcome to Javatpoint.”
)
-
# saving the image file
-
qr_img.save(
“qr-img.jpg”
)
Output:
The explanation for this is .
In the line of code that you just saw, we imported the qrcode library and declared a variable that would produce a QR code by using the make() method that is included inside the qrcode library. After that, we have used the save() method in order to save the code inside the directory.
The preceding QR code may be scanned by using the smartphone in our possession. Warning: Do not use your smartphone to examine QR codes obtained at random, since these codes might contain harmful information.
code/links.
Generating an Advanced QR Code
The programmers have the ability to personalize a QR code by making use of a QRCode object, which is comprised of the parameters indicated in the following.
table:
S.No. | Parameter | Description |
---|---|---|
1 |
version |
There are forty (40) versions of the QR code, which regulates the size of the code. Version 1 is the smallest, whereas version 40 is the largest. Version 1 will generate a 21×21 matrix QR code. |
2 |
error_correction |
This parameter is used to control the Error Correction utilized for the QR code. This varies from 7% to 30% correction of the errors as below:
|
3 |
box_size |
This parameter is used to regulate the number of pixels in the individual block of the QR code. |
4 |
border |
This parameter is used to control the thickness of the border. The default border is 4 pixels thick. |
In order to generate a QR code, we may make use of the following methods that are available on the QRCode object.
code.
S.No. | Functions | Description |
---|---|---|
1 |
add_data() |
We can pass the content of the QR code as a parameter to this function. |
2 |
make() |
In case where we are not sure about which version of QR code to utilize, we can set automatically the version by:
|
3 |
make_image() |
This function is used to generate the QR code. We can also use it in order to set the fill color and background color of the QR code with the help of the fill_color and back_color parameters. |
In order to produce a QR code that directs users to the Python lesson, let us examine the following example. Example:
-
# importing the qrcode module
-
import
qrcode
-
# creating a QRCode object
-
obj_qr = qrcode.QRCode(
-
version =
1
,
-
error_correction = qrcode.constants.ERROR_CORRECT_L,
-
box_size =
10
,
-
border =
4
,
-
)
-
# using the add_data() function
-
obj_qr.add_data(
“https://www.javatpoint.com/python-tutorial”
)
-
# using the make() function
-
obj_qr.make(fit = True)
-
# using the make_image() function
-
qr_img = obj_qr.make_image(fill_color =
“cyan”
, back_color =
“black”
)
-
# saving the QR code image
-
qr_img.save(
“qr-img1.png”
)
Output:
Explanation: Related:
The qrcode library has been imported into the code snippet that was just shown to you. After that, an instance of the QRCode class that is included inside the qrcode library was generated by us. To achieve the desired level of personalization for the QR code, we made use of a variety of factors. After that, we have included the information for the QR code via the usage of the add data() method. In addition to that, we have generated the image of the QR code by using the create() and make image() methods. At the end, we were successful in saving the picture file in the directory by utilizing the save button ()
function.
How to read a QR Code?
In order to read the QR code, we are going to make use of the OpenCV library. If the package is not already present on the system, we may use the following command to install it with the help of the pip installer if it is not already present: Syntax:
-
$ pip install cv2
When we have finished installing everything, we can go on to the next part of the QR code, which is decoding it. In order to decipher the code, we are going to make use of the detectAndDecode method that is included in the QRCodeDetector object that OpenCV provides.
Let’s have a look at the piece of code for the same thing, shall we? Example:
-
# importing the OpenCV library
-
import
cv2
-
# reading the image
-
qr_img = cv2.imread(
“qr-img1.png”
)
-
# using the QRCodeDetector() function
-
qr_det = cv2.QRCodeDetector()
-
# using the detectAndDecode() function
-
val, pts, st_code = qr_det.detectAndDecode(qr_img)
-
# printing the value
-
print(
“Information:”
, val)
Output:
Explanation for
Information: https://www.javatpoint.com/python-tutorial
The cv2 library has been imported into the code snippet that was just shown to you. The picture was then read from the directory using the imread() method, and the QR code was located inside the image using the QRCodeDectector() function. After that, we have made use of the detectAndDecode() method, and we have printed the value out for the users.
As a consequence of this, the detectAndDecode method will return the information that is included inside the QR code, as well as the coordinates of the four corners of the box and a binarized version of the QR code.