This article aims to introduce you to using the python library: qrtools. This library can be used to both read QR codes and generate them.
What are QR codes?
QR Code or a quick response code is a trademark for a two-dimensional barcode type. 2D barcodes are similar to 1D barcodes, but can store more information per unit area.
Installation and Dependencies
- Debian Linux: qrtools can be installed on Debian based Linux systems with the following commands
sudo apt-get update sudo apt-get install python-qrtools
Must also be installed the following dependencies
[sudo] pip install pypng [sudo] pip install zbar [sudo] pip install pillow
- Windows: qrtools can be installed on Windows by downloading the file from here . While downloading and extracting, run the following command from
python setup.py install
Generate QR Codefolder
qrtools contains a QR class (can be viewed in the source code) for which we must initially create an object. The object takes the following arguments
- data type
To create a QR code with default settings, we just need to specify the data when creating the object. Note that the data must be a Unicode object if non-ASCII objects are to be used.
If the program runs successfully , it returns 0 and the QR code is saved in the tmp folder. To find out the exact location, use the following command
This file can now be moved to another folder according to our convenience
The pixel value of the QR code can also be changed by specifying the value during QR creation -object. The default size is usually a little small for reading with smartphones scanners, so a size around 10 would be ideal for such purposes, for example:
my_QR = QR (data = u "example", pixel_size = 10)
The below QR code has pixel size = 10 and was URL encoded
We can also add email data, SMS data, MMS data, bookmarks, etc. D. Into the QR code. The following code snippet is taken from the source code, which indicates the different types of data that can be used, as well as the format of the data that will be required to use it:
From the above code, we can see the different types of data that can be assigned and used when generating QR codes. For example, to use a bookmark as data, we must provide the data as a list of title and URL. To do this, do the following
Read QR code
Scan and read QR code relatively simple. When creating a QR object, we just have to provide the path to the QR code as an argument. Let’s say we are trying to decode the QR code generated at the beginning of the article.
We can also print the values of other parameters passed when creating a QR object to generate a QR code, for example using the same QR code generated at the beginning of the article, adding these print operators will additionally give the following additional output
print my_QR.data_type print my_QR.pixel_size print my_QR.margin_size
text 3 4
This article is provided by Deepak Srivatsav . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.