Skip to content

Instantly share code, notes, and snippets.

@kiyota-yoji
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiyota-yoji/ae2d9eebc89997d69178 to your computer and use it in GitHub Desktop.
Save kiyota-yoji/ae2d9eebc89997d69178 to your computer and use it in GitHub Desktop.
Pythonで画像ファイルからQRコードを読み込む (MacOS) ref: http://qiita.com/kiyota-yoji/items/7fe134a64177ed708fdd
$ brew install zbar
$ 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
$ 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
>>>
$ pip install Pillow
$ ./zbar_test.py /path/to/image.jpg
decoded QRCODE symbol "http://www.example.com/"
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment