The Mouse Vs. The Python

Python Programming from the Frontlines

The other day, I thought it would be fun to create a little program that could generate QR codes and show them onscreen with wxPython. Of course, I wanted to do it all with Python, so after a little looking, I came across 3 candidates:

I tried python-qrcode and pyqrnative since they worked on Windows as well as Mac and Linux. They also didn’t require anything except the Python Imaging Library. The pyqrcode project requires several other prerequisites and didn’t work on Windows, so I didn’t want to mess with it. I ended up taking some old code based on my Photo Viewer application and modified it slightly to make this a QR Code viewer. If I’ve piqued your interest, then read on!

Getting Started

As I noted at the beginning, you will need the Python Imaging Library. We’ll be using wxPython for the GUI part, so you’ll need that as well. And you’ll want to download python-qrcode and pyqrnative. The main difference I have found is that python-qrcode is much faster at generating the images and it generates the type you’ve probably seen the most of. For some reason, pyqrnative take a lot longer to run and it creates a much denser looking QR code. There may be options for both of these projects that allow you to generate different kinds of codes, but the documentation for either project is abysmal. I ended up using the source and Wingware’s IDE to traverse the code more than anything else.

Generating QR Codes

Anyway, once you have all the prerequisites, you can run the following code and see what Python can do:

The code for changing and showing the picture is explained in the previous article I wrote (and linked to above), so the only parts that you’ll probably care about are the two methods for generating the QR codes: onUseQrcode and onUsePyQR. I just took some examples from their respective websites and modified them slightly to create the QR code images. They’re very straight-forward, but not well documented, so I can’t really tell you what’s going on. Sadly at the time of this writing, the code for these projects is seriously lacking in docstrings, with only a few here and there. Still, I was able to generate some decent QR codes. The following was done using python-qrcode:

As you can see, it’s a pretty standard code. The next one is created with PyQRNative and is much denser looking:

I tried scanning both images with my Android cell phone’s barcode scanning application and both QR codes were read correctly by it. So if you’re in need of generating QR code images for your project, I hope one of these projects will fit your needs!

UPDATE 5/21/2012

One of my readers (Mike Farmer) contacted me recently about his experiments with PyQRNative and told me that the “first argument is container size and the second is redundancy/error
correction”. I kind of guessed what the second one was, but I don’t really know what the error correction levels do. Fortunately, Mr. Farmer explained it to me: If the error correction is low it will not be able to tolerate smears or tears to the tag without failing to read. But if you crank up the error level it will obviously get bigger qrcode but what you have done is create duplicate data inside the tag. So if the tag is smeared or torn it can still read and recover the remaining data. So if your application is creating tags that can be damaged its wise to crank up the error correction. You can also do cool thing with this like superimpose pictures or text on the tag by cranking error correction up so the data is redundant and then it can tolerate the “damage”. Anyway, if you change the first number, you can grow or shrink the QR code image size. Why would you do that? Well, the more information you need to store in the image, the bigger the image will need to be. Mr. Farmer came up with some fun test code to help him figure out exactly what the minimum size a QR code has to be. I am reproducing the code below:

Source Code

A nice demo, I might check out python-qrcode in the near future. For the more dense QR code, I took a look through their code but it’s… cryptic. I would expect the low error correction level to give you a relatively compact code if my general understanding is correct.

Koen

The second one doesn’t scan correctly on my phone – it identifies it as a number.