Read one-dimensional barcodes and QR codes from Python 2 and 3.
Project description
Read one-dimensional barcodes and QR codes from Python 2 and 3 using the zbar library. Works with PIL / Pillow images, OpenCV / numpy ndarrays, and raw bytes.
The older zbar package is stuck in Python 2.x-land. The zbarlight package doesn’t provide support for Windows and depends upon Pillow. This pure-Python package uses ctypes to bring zbar to Python 2.7 and Python 3.4 or greater.
Installation
The zbar DLLs are included with the Windows Python wheels. On other operating systems, you will need to install the zbar shared library.
Mac OS X:
brew install zbar
Linux:
sudo apt-get install libzbar0
Install this Python wrapper; use the second form to install dependencies of the command-line scripts:
pip install pyzbar pip install pyzbar[scripts]
Example usage
The decode function accepts instances of PIL.Image.
>>> from pyzbar.pyzbar import decode >>> from PIL import Image >>> decode(Image.open('pyzbar/tests/code128.png')) [Decoded(data=b'Foramenifera', type='CODE128', rect=Rect(left=37, top=550, width=324, height=76)), Decoded(data=b'Rana temporaria', type='CODE128', rect=Rect(left=4, top=0, width=390, height=76))]
It also accepts instances of numpy.ndarray, which might come from loading images using OpenCV.
>>> import cv2 >>> decode(cv2.imread('pyzbar/tests/code128.png')) [Decoded(data=b'Foramenifera', type='CODE128', rect=Rect(left=37, top=550, width=324, height=76)), Decoded(data=b'Rana temporaria', type='CODE128', rect=Rect(left=4, top=0, width=390, height=76))]
You can also provide a tuple (pixels, width, height), where the image data is eight bits-per-pixel.
>>> image = cv2.imread('pyzbar/tests/code128.png') >>> height, width = image.shape[:2] >>> # 8 bpp by considering just the blue channel >>> decode((image[:, :, 0].astype('uint8').tobytes(), width, height)) [Decoded(data=b'Foramenifera', type='CODE128', rect=Rect(left=37, top=550, width=324, height=76)), Decoded(data=b'Rana temporaria', type='CODE128', rect=Rect(left=4, top=0, width=390, height=76))] >>> # 8 bpp by converting image to greyscale >>> grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) >>> decode((grey.tobytes(), width, height)) [Decoded(data=b'Foramenifera', type='CODE128', rect=Rect(left=37, top=550, width=324, height=76)), Decoded(data=b'Rana temporaria', type='CODE128', rect=Rect(left=4, top=0, width=390, height=76))] >>> # If you don't provide 8 bpp >>> decode((image.tobytes(), width, height)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/lawh/projects/pyzbar/pyzbar/pyzbar.py", line 102, in decode raise PyZbarError('Unsupported bits-per-pixel [{0}]'.format(bpp)) pyzbar.pyzbar_error.PyZbarError: Unsupported bits-per-pixel [24]
The default behaviour is to decode all symbol types. You can look for just your symbol types
>>> from pyzbar.pyzbar import ZBarSymbol >>> # Look for just qrcode >>> decode(Image.open('pyzbar/tests/qrcode.png'), symbols=[ZBarSymbol.QRCODE]) [Decoded(data=b'Thalassiodracon', type='QRCODE', rect=Rect(left=27, top=27, width=145, height=145))] >>> # If we look for just code128, the qrcodes in the image will not be detected >>> decode(Image.open('pyzbar/tests/qrcode.png'), symbols=[ZBarSymbol.CODE128]) []
Windows error message
If you see an ugly ImportError when importing pyzbar on Windows you will most likely need the Visual C++ Redistributable Packages for Visual Studio 2013. Install vcredist_x64.exe if using 64-bit Python, vcredist_x86.exe if using 32-bit Python.
License
pyzbar is distributed under the MIT license (see LICENCE.txt). The zbar shared library is distributed under the GNU Lesser General Public License, version 2.1 (see zbar-LICENCE.txt).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for pyzbar-0.1.5-py2.py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a44fd290cc90f8dfef58d46ae4dc1635ca42b97503332f12023b532de36c14f8 |
|
MD5 | 3e53b5c647072908334db821ebda8200 |
|
BLAKE2b-256 | 2c71e4d6cab60d28565d8bdd0a1b90d86230d27ba7f93d1bba5a3911cb9dc82d |
Hashes for pyzbar-0.1.5-py2.py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff8b5522521c62ee794f8f386ac4e8b1569b7e95a8f3ceb46125bf0ad3ba494e |
|
MD5 | 769c248c1c86d385b1c416dbdc708fea |
|
BLAKE2b-256 | 0448dc5158692556ea13722b9a9137cf98c3a0ab803195973c5be19f08b8b3b1 |
Hashes for pyzbar-0.1.5-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f945412e075bcfab8cedde1e1d384d41001b2a8e78b092ec0dfe1d970b2d87c |
|
MD5 | 75e061acd4b50f8b74100d0b2a3ae83f |
|
BLAKE2b-256 | 37fc5413d9b1eb0bd501ed184b18ac1d632bec727020ee034c79bf2c4828449a |