PythonでQRコードのスキャン方法

PythonでQRコードの読み取り方

使用するライブラリ

ZBar
http://zbar.sourceforge.net/

Pillow (Python Imaging Library)
http://python-pillow.github.io/

使い方

QRコードの画像qrcode.pngを読み込む

>>> from PIL import Image
>>> pil = Image.open('qrcode.png').convert('L')
>>> width, height = pil.size
>>> data = pil.tobytes()

tostring()は廃止されたので、代わりにtobytes()を使います。

ZBarを使ってQRコードをスキャン

>>> import zbar
>>> scanner = zbar.ImageScanner()
>>> scanner.parse_config('enable')
>>> image = zbar.Image(width, height, 'Y800', data)
>>> scanner.scan(image)

スキャンした内容

>>> for symbol in image:
>>>   print symbol.type, ':', symbol.data

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です