Use ZBar and the Python module https://pypi.python.org/pypi/zbar/. It also supports barcodes other than QR codes.
In order to streamline the aggregation of experimental results using paper cards, I made a note that I prepared a QR code in the card in advance and automatically processed it with Python after scanning.
ZBar
One-shot installation with Homebrew.
$ brew install zbar
I'd like to install this with PyPI in one shot, but if I put it in as it is, Python will die with a Segmentation fault at the time of import.
$ pip install zbar
$ python
Python 2.7.9 (default, Jan 7 2015, 11:50:42)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zbar
Segmentation fault: 11
$ pip uninstall zbar
Therefore, apply Patch to the source code and install it. In this patch, we add sentinel to the array to prevent the occurrence of Segmantation fault.
$ wget https://pypi.python.org/packages/source/z/zbar/zbar-0.10.tar.bz2
$ wget https://github.com/npinchot/zbar/commit/d3c1611ad2411fbdc3e79eb96ca704a63d30ae69.diff
$ tar jxvf zbar-0.10.tar.bz2
$ cd zbar-0.10
$ patch -p1 < ../d3c1611ad2411fbdc3e79eb96ca704a63d30ae69.diff
patching file imagescanner.c
$ python setup.py install
$ python
Python 2.7.9 (default, Jan 7 2015, 11:50:42)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>>
This time it was okay.
Enter with one PyPI.
$ pip install Pillow
Python test code.
zbar_test.py
zbar_test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
### cf. http://99blues.dyndns.org/blog/2010/12/zbar/
import sys
import zbar
import PIL.Image
if len(sys.argv) < 2: exit(1)
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
# obtain image data
pil = PIL.Image.open(sys.argv[1]).convert('L')
(width, height) = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
del(image)
$ ./zbar_test.py /path/to/image.jpg
decoded QRCODE symbol "http://www.example.com/"
Recommended Posts