How to decode a QR code image in (preferably clean) Python?

TL; DR : I need a way to decode a QR code from an image file using (preferred clean) Python.

I have a jpg file with a QR code that I want to decode using Python. I found a couple of libraries that claim this:

PyQRCode ( here ), which supposedly can decode qr codes from images by simply providing this path:

import sys, qrcode d = qrcode.Decoder() if d.decode('out.png'): print 'result: ' + d.result else: print 'error: ' + d.error 

So I just installed it with sudo pip install pyqrcode . However, I found that the weird example code above is: it only imports qrcode (not pyqrcode though). Since I think qrcode belongs to this library , which can only generate qr code images that confuse me. So I tried the code above with pyqrcode and qrcode , but both refused on the second line saying AttributeError: 'module' object has no attribute 'Decoder' . In addition, the website belongs to Ubuntu 8.10 (which came out more than 6 years ago), and I cannot find the publication (git or other) repository to check the last commit. So I went to the following library:

ZBar ( website here ) claims to be "an open source software suite for reading bar codes from various sources, such as image files." So I tried installing it on Mac OSX sudo pip install zbar . This fails with error: command 'cc' failed with exit status 1 . I tried the sentences in the answers to this SO question , but I cannot solve it. So I decided to continue again:

QRTools , which according to this blogpost can easily decode images using the following code:

 from qrtools import QR myCode = QR(filename=u"/home/psutton/Documents/Python/qrcodes/qrcode.png") if myCode.decode(): print myCode.data print myCode.data_type print myCode.data_to_string() 

So I tried installing it with sudo pip install qrtools , which did not find anything. I also tried it with python-qrtools , qr-tools , python-qrtools and a few more combinations, but, unfortunately, to no avail. I believe this relates to this repo , which states that it is based on ZBar (see above). Although I want to run my code on Heroku (and thus prefer a clean Python solution), I successfully installed it in a Linux box (with sudo apt-get install python-qrtools ) and tried to run it:

 from qrtools import QR c = QR(filename='/home/kramer65/qrcode.jpg') c.data # prints u'NULL' c.data_type # prints u'text' c.data_to_string() # prints '\xef\xbb\xbfNULL' where I expect an int (being `1234567890`) 

Although this seems to decrypt it, it seems like it is not. Also, it needs ZBar and therefore is not pure Python. So I decided to find another library.

PyXing (the site here ) is supposedly the Python port of the popular ZXing Java library , but the initial and only commit is 6 years, and the project does not have any readme or documentation.

For the rest, I found a couple of qr- ru codes (not de ) and some API endpoints that can be decoded for you. Since I don't like the fact that this service depends on other API endpoints, I would like to keep local decoding.

So, to conclude; Does anyone know how I can decode QR codes from images in (preferred pure) Python? All tips are welcome!

+52
python decode zxing zbar qr-code
Dec 01 '14 at 16:58
source share
2 answers

You can try the following steps and code using qrtools :

  • Create a qrcode file if it does not already exist

    • For this, I used pyqrcode , which can be installed using pip install pyqrcode
    • And then use the code:

       >>> import pyqrcode >>> qr = pyqrcode.create("HORN OK PLEASE.") >>> qr.png("horn.png", scale=6) 
  • Decode an existing qrcode file using qrtools

    • Install qrtools with sudo apt-get install python-qrtools
    • Now use the following code on the python command line

       >>> import qrtools >>> qr = qrtools.QR() >>> qr.decode("horn.png") >>> print qr.data u'HORN OK PLEASE.' 

Here is the complete code in one go:

 In [2]: import pyqrcode In [3]: qr = pyqrcode.create("HORN OK PLEASE.") In [4]: qr.png("horn.png", scale=6) In [5]: import qrtools In [6]: qr = qrtools.QR() In [7]: qr.decode("horn.png") Out[7]: True In [8]: print qr.data HORN OK PLEASE. 

Warning

  • You may need to install PyPNG with pip install pypng to use pyqrcode
  • If you installed PIL , you can get IOError: decoder zip not available . In this case, try uninstalling and reinstalling PIL using:

     pip uninstall PIL pip install PIL 
  • If this does not work, try using Pillow instead.

     pip uninstall PIL pip install pillow 
+72
Dec 04 '14 at 12:24
source share

I spent almost half an hour getting it working on Windows + Python 2.7 64-bit, so here are some additional notes to the accepted answer:

and the code from the main answer should work:

 import pyqrcode qr = pyqrcode.create("HORN OK PLEASE.") qr.png("horn.png", scale=6) import qrtools qr = qrtools.QR() qr.decode("horn.png") print qr.data 
+2
May 20 '18 at 14:38
source share



All Articles