Reading and Generating QR codes in Python using QRtools
Last Updated :
03 Jul, 2022
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
- data
- pixel_size
- level
- margin_size
- data_type
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.
from qrtools
import QR
my_QR = QR(data = u "Example" )
my_QR.encode()
|
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
print (my_QR.filename)
Sample output
/tmp/qr-1496334996.385343/7489ebbcc2a00056ddaaaac190bce473e5c03696ea1bd8ed83cf59a174283862.png

This file can now be moved to another folder as per our convenience
from qrtools import QR
import os
my_QR = QR(data = u "Example" )
my_QR.encode()
os.system( "sudo mv " + my_QR.filename + " ~/Desktop" )
|
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 data types that can be used along with the format of the data that would be required for its usage:
data_encode = {
'text' : lambda data: data,
'url' : encode_url,
'email' : lambda data: 'mailto:' + re. compile (
r '^mailto:' , re.IGNORECASE
).sub('', data),
'emailmessage' : lambda data: 'MATMSG:TO:' + data[ 0 ] + ';SUB:' + data[ 1 ] +
';BODY:' + data[ 2 ] + ';;' ,
'telephone' : lambda data: 'tel:' + re. compile (
r '^tel:' , re.IGNORECASE
).sub('', data),
'sms' : lambda data: 'SMSTO:' + data[ 0 ] + ':' + data[ 1 ],
'mms' : lambda data: 'MMSTO:' + data[ 0 ] + ':' + data[ 1 ],
'geo' : lambda data: 'geo:' + data[ 0 ] + ', ' + data[ 1 ],
'bookmark' : lambda data: "MEBKM:TITLE:" + data[ 0 ] + ";URL:" +
data[ 1 ] + ";;" ,
'phonebook' : lambda data: "MECARD:" + ";" .join([ ":" .join(i)
for i in data]) + ";"
}
|
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
from qrtools import QR
data_type = 'bookmark' )
my_QR.encode()
|
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.
from qrtools import QR
my_QR = QR(filename = "home/user/Desktop/qr.png" )
my_QR.decode()
print (my_QR.data)
|
Output :
Example

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)
Output:
text
3
4

Similar Reads
Generate QR Code using qrcode in Python
A Quick Response Code or a QR Code is a two-dimensional bar code used for its fast readability and comparatively large storage capacity. It consists of black squares arranged in a square grid on a white background. Python has a library "qrcode" for generating QR code images. It can be installed usin
2 min read
Wi-Fi QR Code Generator Using Python
Prerequisite: Getting Saved Wifi Passwords using Python We know the wireless network is the most common network adapter for today, Because of its supports portability and User friendly. In this article, we will see how we can get the current saved Wi-Fi name and passwords and generate QR code to con
2 min read
Detect and Read Barcodes with OpenCV in Python
A barcode is a graphical representation of data that is machine-readable. It consists of parallel lines or rectangles of varying widths and spacings, along with specific patterns, that encode information. Barcodes are widely used for automatic identification and tracking of products, assets, invento
3 min read
Generating random Id's in Python
Generating random IDs in Python is useful when we need unique identifiers for things like user accounts, sessions, or database entries. In this article we will see Various methods to Generate Random ID's in Python. Using random.randint()random.randint() method in Python is used to generate random in
2 min read
How to Generate Barcode in Python?
In this article, we are going to write a short script to generate barcodes using Python. We'll be using the python-barcode module which is a fork of the pyBarcode module. This module provides us the functionality to generate barcodes in SVG format. Pillow is required to generate barcodes in image fo
2 min read
Python | Generate QR Code using pyqrcode module
Let's see how to generate QR code in Python using pyqrcode module. pyqrcode module is a QR code generator. The module automates most of the building process for creating QR codes. This module attempts to follow the QR code standard as closely as possible. The terminology and the encodings used in py
2 min read
Python - Convert Image to String and vice-versa
To store or transfer an Image to some we need to convert it into a string such that the string should portray the image which we give as input. In Python we have a lot of functions in Python available to convert an image into a string. Image used: Convert Image To StringTo convert an image to a stri
2 min read
Create Random RGB Color Code using Python
Color codes are the defacto method of representing a color. It helps accurately represent the color, regardless of the display calibration. This article will teach you how to create random RGB color code using Python. RGB Color Code An RGB color code is a tuple containing 3 numbers representing Red,
3 min read
PyQt5 QScrollBar â Getting Range Changed Signal
In this article we will see how we can get the range changed signal of QScrollBar. QScrollBar is a control that enables the user to access parts of a document that is larger than the widget used to display it. Slider is the scroll-able object inside the bar. By default value range from 0 to 99. Valu
2 min read
Building QR Code Generator Application using PyQt5
In this article, we will see how we can make a QR code generator application using PyQt5. A QR code is a type of matrix barcode first designed in 1994 for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached.
4 min read