This article was published as a part of the Data Science Blogathon.
QR Code, what a wonderful thing it is. Every product that we buy no matter how large or small, expensive or cheap it is, is powered by a QR code. As per definition, QR-code is a machine-readable optical label that contains information about the item to which it is attached. QR codes contain data either for a locator, identifier, or tracker that points to a website or application. The best part about QR codes is that they can hold up to 4000 characters of text. What if I told you that you can create such a QR-Code by using python and that too by using only a few lines of code. So let’s get started!
Following are the pre-requisites needed for this project:
1.Python3
2. Any IDE of your choice
3. Anaconda Distribution (Optional )
If you already have Python installed on your system, then there is no need to install Anaconda Distribution. If not I would suggest you install anaconda distribution. There are many advantages of installing anaconda distribution one being access to jupyter notebook which is a widely used notebook-based IDE for data science projects. Another advantage is when you install the anaconda distribution, it will also install python as a part of the installation sequence. Hence if you are working or want to work in data science, I would suggest anaconda distribution for the same.
For this project, I am going to use a jupyter notebook however there are no fixed criteria to use only the jupyter notebook itself. Any IDE of your own choice can be used.
Following are the libraries which we are going to need:
1.pyqrcode
2.PyPng
Speaking of the first module, the main job of PyQRcode as the name implied is the creation of a QR Code. The second module which is PyPng is used for representing our QR Code as a png image.
1. Importing PyQRcode and PyPng modules
2. Embedding data into a variable
3. Converting data stored in a variable into a QR code
4. Creating image file of QR Code
5. Generating QR code image in the console itself(optional)
As the name implied, in this step we are going to import PyQRode and PyPng modules needed for this project. If you don’t happen to have those modules already installed, the following are the commands to install the same.
pyqrcode
#code
pip install pyqrcode
pypng
#code
pip install pypng
Now let’s get some familiarity with the modules that we are going to use in this project.
pyqrcode is a library that is basically a QR Code Generator and is written in pure python. This module automates the process of creating QR Codes.
On the other hand, pypng is a pythonic implementation of the png module. Its main responsibility is the creation of a png image file.
In this step, we are going to embed our data into a variable. You can put any data of your choice. Usually, QR codes can store web URLs, numbers, or up to 4000 characters of text. In this case, I am going to embed an URL into a variable.
link = "https://www.google.com"
As we can see in the above code, I have created a variable named “link”. In this variable, I have encoded an URL of google.com
In this step, we are going to convert the data which is embedded into a variable into a QR code.
#code
qr_code = pyqrcode.create(link)
As we can see in the above code, we have invoked create() method of the PyQRcode module. This module accepts one argument which is basically the variable in which we have embedded our data. The result of this entire computation is stored in a variable named qr_code.
This is the final step of our project. Prior to this step, we converted our data into QR codes. Now it’s time to display our QR code. Yay!!
Remember the PyPng module that we have imported. Well, there is a method named png using which we are going to generate our QR code image. This method accepts two arguments. The first is the name of the QR code file followed by the extension. The second argument is the scale which determines the size of the resulting QR code image.
scale is a configurable argument using which we can change the size of the QR code image. The higher the value of scale, the larger the resulting QR code image.
And we are done. We have successfully generated QR codes from our data. However, you have a question now -“Where is the QR code ?”.
Now there is one important thing to remember. The output QR code file is going to be generated in a location where our code is getting executed. To make sure that we can easily find our QR code file, set the working location prior to executing the code.
#code
import os os.chdir("C://DataScience/")
By using this code, here we have changed our working location to the DataScience folder in C: drive. The advantage of setting this working location is that, since we have already set our location, our QR code image is going to be generated in this location which eases our task of finding this file.
QR Code Output File
As you can see here, this is the output QR code generated by our code. Now scan this QR code with any QR code scanner and see where it redirects you :).
If you want to display the QR code in the console itself, we have to add an additional line in our code.
#code
print(qr_code.terminal(module_color="black",background="white"))
In this code, the terminal method is used to generate a QR code in the console itself. This method accepts two arguments as input. The first is module_color which decides the color of the pattern inside the QR code. The second is the background which decides the background color of the QR code.
#Changing folder location import os os.chdir("C://DataScience/")
#QR Code Generation import pyqrcode import png link = "https://www.google.com" qr_code = pyqrcode.create(link) qr_code.png("google.png",scale = 5) print(qr_code.terminal(module_color="black",background="white"))
In this article, we have seen how to leverage python’s computational prowess in the generation of QR codes. We have also seen how to embed our data into QR codes.
Following are the takeaways from this article:-
1. QR codes generation can be automated using Python
2.PyQRCode is a python module that is used for generating QR Codes
3.PyPng module is responsible for creating a png file for the QR codes
4. Using the terminal() method, QR code can be generated in the jupyter notebook itself
Hope you like this article.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.