How to create and decode a QR Code in Python using qrtools

This is a mini-How-to for creating and decoding QR Codes in Python using the qr-tools library.

What you need…

First of all you need, you can get it from the QR Toosl Launchpad page:

or you can install it from the QR Tools Stable PPA if you happen to be using Ubuntu:

If you go with the PPA, you can check that qrtools is available in your system like this:

  1. Open a terminal and execute: python
  2. Once you are into python, write import qrtools and press enter; if you don’t get any message everything is OK.
If you choose to download, put the file in the same directory where your script is going to be, for example: /home/me/myproject/.
Once you have the qrtools library available from your python enviroment, we can go on with the next steps.

Generating a Code

The first step to generate a code is to create an instance of the QR class implemented in If we see the class source code in, the __init__ method definition of the QR class looks like this:
def __init__(
self, data=u'NULL', pixel_size=3, level='L', margin_size=4, data_type=u'text', filename=None
The arguments are self-explanatory. For creating a code with the default configuration (data type, pixel size and margin size) we just need to pass the data argument with the text we want to encode:
from qrtools import QR
myCode = QR(data=u"My sample text")
Please notice that the data argument must be a unicode object if you are going to use non-ascii characters. To this point we haven’t created the code yet, to encode the text we need to call the encode() method of our new QR object:
If everything goes ok, the method will return a zero and the myCode object will now have a filename property associated:
print myCode.filename
The encode() method calls the qrencode binary, this binary generates a .png file with the QR Code in a temporary folder, the filename atribute of the QR object points to this file. You can then copy that file to a desired directory or show it in your application. You can modify the attributes of the generated QR Code passing personalized values to the other parameters of the __init__ method, for example, if you want a bigger pixel size of 10, you have to generate the code like this:
from qrtools import QR
myCode = QR(data=u"My sample text data", pixel_size=10)
QR Tools make simple to create QR Codes that contain more complex data like sms, email messages, contact information, etc. If you want to encode a bookmark for example, you have two options:
1. Write the bookmark format and encode it using the text data type like this:
from qrtools import QR
bookmark=u"EBKM:TITLE:My Blog;URL:;;"
myCode = QR(data=bookmark)
2. Use the predefined bookmark data tpye:
from qrtools import QR
bookmark=[u"My Blog", u""]
myCode = QR(data=bookmark,data_type='bookmark')

Note that the data parameter is now a list of unicode objects! You can do this with several data types, this are the ones supported now and the data parameter you have to pass:

  • text: an unicode string with any text you want.
  • url: an unicode string with an url
  • email: an unicode string with an email address
  • emailmessage: a list of unicode strings like [u”email@address”, u”Subject”, u”Message Body”]
  • telephone: an unicode string with the telephone number
  • sms: a list of unicode strings like [u”Telephone Number”, u”Message”]
  • mms: a list of unicode strings like [u”Telephone Number”, u”Message”]
  • geo: a list of unicode strings like [u”latitude”, u”longitude”]
  • bookmark: a list of unicode strings like [u”Title”,u”URL Address”]
  • phonebook: a list of unicode strings like [u”Name”,u”Telephone”, u”E-Mail”] (This one in particular is still under development, so expect some changes).

Decoding from a File

To decode from a file the procedure we have to follow is the following. First, like the previous case, we have to create an instance of the QR Class, but now we need to pass the filename parameter with the route to the file we want to decode, say we have a sample.png file in our desktop we want to decode. We’ll do something like this:
from qrtools import QR
myCode = QR(filename=u"/home/me/Desktop/sample.png")
if myCode.decode():
  print myCode.data_type
  print myCode.data_to_string()
Notice that the decode method returns True when the decoding was successful. There are several properties of the myCode object you can test, I invite you to take a look at the code and try!
That’s all for decoding from a file! 🙂 so.. let’s move on to the last part of this tutorial..
Decoding from WebCam
Decoding from webcam is even easier than from a file, you just have to create an instance and call the decode_webcam() method:
from qrtools import QR
myCode = QR()
print myCode.decode_webcam()
The decode_webcam() method will open a zbar window that will let you scan for a code using your webcam 🙂 once you got a green frame over your code press a key and the window will close. The method returns “NULL” when nothing could be decoded.
That all for the tutorial, I hope you find it useful, please let me know in the comments. 😀

34 pensamientos en “How to create and decode a QR Code in Python using qrtools

  1. ciao, sto provando a seguire questo tutorial ma i primi link sono scollegati e non riesco a seguire tutta la procedura – potresti aiutarmi e indicarmi quale file scaricare? io vorrei usare un rpi per scansionare l’entrata di un associazione potesti aiutarmi?

  2. hello, I’m trying to follow this tutorial but the first link is disconnected and can not seem to follow the entire procedure – could you help me and tell me which file to download? I would like to use a rpi to scan the entry of an association could help me?

    • You’ll need the ‘Decode From File’ section. Get a picture of the qr code you want to scan on to your Pi, then just follow the tutorial. Or, you could use ‘Decode from WebCam’ and decode from a camera attatched to the Pi.

  3. Pingback: TuxLux: QR-Steuerkarten erstellen | Knuermpfhausen

  4. Help me experts.

    above two pictures are very similar. (1. png formatted 380×380 pixels 45×45 qrcode image, 2. png formatted 470×470 pixels 45×45 qrcode image) qrtools decoded successfully former one, and couldn’t latter one. what is the reason qrtools cant decode second image. Please light my brain.

  5. iam trying to decode qrcode from webcam. it is identifying the qrcode by showing green broader but always giving ‘none’ as return value. please help me out

  6. Thank you! I’m learning the information you gave. But shouldn’t I be looking at the definition of ‘decode_webcam()’ function in ‘python2.7/’ file? I’ll let you know if I figured out. Also is qrtools a new project? I can’t find much information online.

    • Yes, is the lib; in brief you should create an instance of the QR object, let’s say you name it “qr”. Then qr.decode_webcam(device=’/dev/yourwebcam’) method of the instance opens the zbar window for decoding using your webcam and stores the info on the qr object instance. You can access the readed information using and you can decode it using qr.data_decode[qr.data_type](

      Regarding your question, qrtools is quite “old”, it has something like 3 years without maintenance, it started just as a GUI to decode QR codes and ended up beeing the lib + the Qt GUI.

      All that there is are my post and the information on launchpad. qrtools 2.0 (and QtQR 2.0) is on hold right now.

      • Hi @ralgozino I found out that the “” stores the data. I just print it out. Now it prints the data of qrcode every time has greenbox on the webcam! And returns None when nothing to decode. Great

  7. Hi Ralgozino, i have the same problem with the myCode.decode_webcam(). Green Box, but in the terminal i just have “None” 😦 any idea of how to fix it?

    • Hola Laurita, pudiste decodificar? fijate que en el objeto myCode, tenés un método “decode” que decodifica lo que tiene el código, ejemplo:
      y luego en tenés la info.

  8. Gracias!!!!!!! Excelente, me sirvió total!! te comentare mi actual código
    “from qrtools import QR
    myCode = QR( )

    print myCode.decode_webcam( )

    A la hora de ejecutarlo muestra en la terminal “None ” y descodifica el QR… algo así
    (el texto codificado del QR)”

    me sirve el hecho de que muestre en la terminal la 2 linea, mas no la primera ósea “None”. Tengo entendido de que debo de eliminar en el código de python la siguiente linea “print myCode.decode_webcam( )” pero si lo hago me sale error…

    No es un problema de gran magnitud, es de simple estetica. Espero tu pronta respuesta!!!
    haces un gran trabajo 🙂

    • Laurita, eso soluciona quitando el print, que no es necesario. Es decir:
      from qrtools import QR
      myCode = QR( )

      myCode.decode_webcam( )


  9. hi !!
    from qrtools import QR
    import zbar

    myCode.data_decode [myCode.data_type] (

    as i tried this i am getting a type error that śtr’ is not callable. please explain the reason behind this.
    i am ussing jessie on my raspberry pi

  10. Hello! I have a error:
    in line 158 scanner = zbar.ImageScanner()
    AttributeError: module ‘zbar’ has no attribute ‘ImageScanner’

    can you help me with my exeption?


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios .