Skip to main content

A ctypes-based wrapper around the zbar barcode reader.

Project description

Python Versions PyPI version Travis status Coverage Status

A ctypes-based Python wrapper around the zbar barcode reader.

The zbar wrapper is stuck in Python 2.x-land. The zbarlight wrapper doesn’t provide support for Windows and depends upon Pillow. This ctypes-based wrapper brings zbar to Python 2.7 and to 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.

On Mac OS X:

brew install zbar

On 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'),
 Decoded(data=b'Rana temporaria', type='CODE128')]

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'),
 Decoded(data=b'Rana temporaria', type='CODE128')]

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'),
 Decoded(data=b'Rana temporaria', type='CODE128')]

>>> # 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'),
 Decoded(data=b'Rana temporaria', type='CODE128')]

>>> # 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]

zbar’s default behaviour is (I think) to decode all symbol types. You can ask zbar to look for just your symbol types (I have no idea of the effect of this on performance)

>>> 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')]

>>> # 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])
[]

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyzbar-0.1.2-py2.py3-none-win_amd64.whl (1.6 MB view details)

Uploaded Python 2Python 3Windows x86-64

pyzbar-0.1.2-py2.py3-none-win32.whl (800.0 kB view details)

Uploaded Python 2Python 3Windows x86

pyzbar-0.1.2-py2.py3-none-any.whl (21.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyzbar-0.1.2-py2.py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for pyzbar-0.1.2-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 3af5a3bede5e65a97fa3b820fa77b932c89d10e7c7e512b76ff0eec0addcdb8d
MD5 f6ad34b2005ad5f124d8a6783a166491
BLAKE2b-256 a9dd6dd2ab89c875900240b08711c15e0ed663cbd00ffdcd72e49311fbd53389

See more details on using hashes here.

File details

Details for the file pyzbar-0.1.2-py2.py3-none-win32.whl.

File metadata

File hashes

Hashes for pyzbar-0.1.2-py2.py3-none-win32.whl
Algorithm Hash digest
SHA256 2e6ca1c385a6ae7ba0eddb1d8bc4b80a5c15c1f7feaf3e3f5b30036ebb8ca85a
MD5 396ca7baf51d2e09a636be36cd74a0e4
BLAKE2b-256 9960d31a9e3dbeeb1acd0bd608e9205dbc58e313c5b281a5cb127286c689d07a

See more details on using hashes here.

File details

Details for the file pyzbar-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pyzbar-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 22abcbfdad4f3162443ca296f050c4ee0694950d5ff9d8c56463aeed0cd5875f
MD5 0576ca3106f69285e215000fb5323d51
BLAKE2b-256 ec7ef2658bd471044a4ae4c3d13cb1224b11e85ad0f91a325ed6fa664e22c005

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page