Change language

Reading and generating QR codes in Python using QRtools

| |

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

  1. 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 
  2. Windows: qrtools can be installed on Windows by downloading the file from here . While downloading and extracting, run the following command from
     python install 

Generate QR Code


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

  1. data
  2. pixel_size
  3. level
  4. margin_size
  5. 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.

# Python 2.x QR code generator

from qrtools

import QR

# creates QR object

my_QR = QR ( data = u "Example" )

# encodes into QR code
my_QR.encode ()

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

 print my_QR.filename 

Sample output

 /tmp/qr-1496334996.385343/7489ebbcc2a00056ddaaaac190bce473e5c03696ea2859ad817 .png 

This file can now be moved to another folder according to our convenience

# Python 2.x QR code generator

from qrtools import QR


import os

my_QR = QR (data = u "Example" )

my_QR.encode ()

# command to move the QR code to the desktop

os.system ( "sudo mv" + my_QR.filename + "~ / Desktop" )

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:

# use them for custom data formats, for example. URL, phone number, VCARD
# data must be a Unicode object or a list of Unicode objects

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 or meCard must be a list of such tuples:

# [(& # 39; N & # 39 ;, & # 39; Name & # 39;), (& # 39; TEL & # 39;, & # 39; 231698890 & # 39;), ...]

’phonebook’ : lambda data: "MECARD:" + "; " . join ([ ": " . join (i) 

for i in data]) + "; "


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

# Python 2.x QR code generator

from qrtools import QR

# creates a QR object

my_QR = QR (data = [u "pythonengineering" , u " " ], 

data_type = ’bookmark’ )

# encodes into QR code
my_QR.encode ()

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.

# Python 2.x program for scanning and reading QR codes

from qrtools import QR

my_QR = QR (filename = "home / user / Desktop / qr.png" )

# decodes the QR code and returns True on success
my_QR.decode ()

# prints data




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 

Exit :

 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 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.


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method