Generate a QR Code using Python
Understanding the QR Code
In the following tutorial, we will learn to generate and read a QR code using the Python programming language.
So, let's get started.
Generating QR Code using Python
Python is a programming language that provides different modules and packages that allow us to generate a QR code. For this tutorial, we will be working with the qrcode package in order to generate the code.
However, in order to start working with the package, we have to install it.
Installing the Python qrcode package
We can install the qrcode package with the help of the pip installer using the following command:
The package will be installed in the system as the version of Python and pip.
Verifying the Installation
In order to check whether the package has been installed in the system properly or not, we can try importing the package and execute the program.
Once the installation is complete, create a new Python file and type the following syntax in it.
Now, save the file and run the file using the following command in the command prompt.
If the program runs without raising any import error, the module is installed properly. Else it is recommended to reinstall the package and refer to its official documentation.
Now, let us understand start working with the qrcode library.
Generating a Simple QR Code
We can generate a simple QR code using the make function of qrcode and pass the data as its parameter.
Let us consider the following example that produces a QR code that reads "Welcome to Javatpoint".
In the above snippet of code, we have imported the qrcode library and defined a variable that uses the make() function of the qrcode library to generate a QR code. We have then saved the code using the save() function in the directory.
We can use the smartphone in order to read the above QR code.
Caution: Do not use the smartphone to read random QR codes because they may contain malicious code/links.
Generating an Advanced QR Code
The programmers can customize a QR code using a QRCode object which consists of the parameters shown in the following table:
We can use the following functions of the QRCode object in order to create a QR code.
Let us consider the following example in order to generate a QR code which points towards the Python tutorial.
In the above snippet of code, we have imported the qrcode library. We have then created an instance of the QRCode class of the qrcode library. We have used different parameters in order to customize the QR code. We have then used the add_data() function to include the information for the QR code. We have also used the make() and make_image() functions to generate the QR code image. At last, we have saved the image file in the directory using the save() function.
How to read a QR Code?
We will utilize the OpenCV library in order to read the QR code. If the package is not installed in the system, we can use the following command in order to install it using the pip installer:
Once the installation is done, we can head onto the decoding section of the QR code. In order to decode the code, we will be using the detectAndDecode function of QRCodeDetector object of OpenCV.
Let us consider the snippet of code for the same.
In the above snippet of code, we have imported the cv2 library. We have then used the imread() function to read the image from the directory and the QRCodeDectector() function to detect the QR code in the image. We have then used the detectAndDecode() function and printed the value for the users.
As a result, the detectAndDecode function returns the content of the QR code, coordinates of the corners of the box, and binarized QR code.
Next TopicBest Python PDF Library
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week