This article aims to introduce the use of the python library: qrtools. This library can be used to both read QR codes and generate them.
What are QR codes?
QR code, or quick response code, is a trademark for a type of 2 dimensional barcode. 2 dimensional barcodes are similar to one dimensional 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
The following dependencies must be installed as well
[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. On downloading and extraction, run the following command from inside the folder
python setup.py install
Generate a QR Code
qrtools contains a class QR (can be viewed in the source code), for which we must initially create an object. The object takes the following arguments
To create a QR code with default settings, we must simply specify the data while creating the object. Note that the data must be a unicode object if non-ASCII objects are going to be used.
If the program runs successfully, it returns a value of 0, and the QR code is stored in the tmp folder. To know the exact location, use the following command
This file can now be moved to another folder as per our convenience
The pixel value of the QR code may also be changed by specifying the value during the creation of the QR object. The default size tends to be a little small for reading using scanners on smartphones, so a size of 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 has been encoded with a URL
We can also add email data, sms data, mms data, bookmarks, etc to the QR code. The following code excerpt is taken from the source code, which specifies the various datatypes that can be used along with the format of the data that would be required for its usage:
From the above code, we observe the various data types that can be assigned and used while creating QR codes. For example, to use a bookmark as data we must provide data as a list, consisting of a title and the url. To accomplish this, we must do the following
Read a QR code
Scanning and reading a QR code is relatively simple. While creating the QR object, we must simply specify the path to the QR code as an argument. Suppose we are trying to decode the QR code created at the beginning of the article.
We may also print the values of the other parameters passed while creating the QR object to generate the QR code, for example, using the same QR code generated at the beginning of the article, additionally adding these print statements would 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 contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Using C codes in Python | Set 1
- Using C codes in Python | Set 2
- Generating random Id's in Python
- Generating Word Cloud in Python | Set 2
- Generating Random id's using UUID in Python
- Generating Strong Password using Python
- Generating Word Cloud in Python
- Generating random number list in Python
- Generating hash id's using uuid3() and uuid5() in Python
- Reading Python File-Like Objects from C | Python
- Reading images in Python
- Python | Reading .ini Configuration Files
- Reading an excel file using Python
- Reading an image in OpenCV using Python
- Reading and Writing to text files in Python