4

On my Ubuntu server I've installed the python-qrtools package (which makes use of zbar) using sudo apt-get install python-qrtools to decode images of QR-codes and bar-codes in Python like this:

>>> qr = qrtools.QR()
>>> qr.decode('the_qrcode_or_barcode_image.jpg')
True
>>> print qr.data
Hello! :)

This works perfectly fine.

I now want to store this data and regenerate the image at a later point in time. But the problem is that I don't know whether the original image was a QR-code or some type of bar-code. I checked all properties of the qr object, but none of them seems to give me the type of Encoding Style (QR/bar/other).

In this SO thread it is described that ZBar does give back the type of Encoding Style, but it only gives an example in Objective-C, plus I'm not sure if this is actually an answer to what I am looking for.

Does anybody know how I can find out the type of Encoding Style (so QR-code/BAR-code/other) in Python (preferably using the python-qrtools package)? And if not in Python, are there any Linux command line tools which can find this out? All tips are welcome!

Community
  • 1
  • 1
kramer65
  • 50,427
  • 120
  • 308
  • 488

1 Answers1

5

Looking at the source of qrtools, I cannot see any way to get the type but there is a zbar python lib, based on the scan_image example, the code below seems to do what you want:

import zbar
import Image

scanner = zbar.ImageScanner()

scanner.parse_config('enable')

img = Image.open("br.png").convert('L')
width, height = img.size
stream = zbar.Image(width, height, 'Y800', img.tostring())

scanner.scan(stream)

for symbol in stream:
    print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data

Using a random barcode I grabbed from the net:

decoded UPCA symbol "123456789012"

Using this qr code outputs:

 decoded QRCODE symbol "http://www.reichmann-racing.de"
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Hi Padraic. Thanks a million for that code! Works like a charm. Just a last question; do you know what the `'Y800'` refers to? – kramer65 Aug 24 '15 at 12:44
  • @Kramer,you're welcome.The third argument is the image format four-character code, the images must be either `"Y800"(greyscale)` or `"GRAY"` for zbar. – Padraic Cunningham Aug 24 '15 at 15:46