Skip to main content

Read one-dimensional barcodes and QR codes from Python 3.

Project description

https://img.shields.io/badge/python-3.5%2C%203.6%2C%203.7%2C%203.8%2C%203.9-blue.svg https://badge.fury.io/py/pyzbar.svg https://travis-ci.org/dferens/pyzbar.svg?branch=master https://coveralls.io/repos/github/dferens/pyzbar/badge.svg?branch=master

Read one-dimensional barcodes and QR codes from Python 3 using the zbar library.

  • Pure python

  • Works with PIL / Pillow images, OpenCV / numpy ndarrays, and raw bytes

  • Decodes locations of barcodes

  • No dependencies, other than the zbar library itself

  • Tested on Python Python 3.5 to 3.9

The older zbar package is stuck in Python 2.x-land. The zbarlight package does not provide support for Windows and depends upon Pillow.

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:

pip install pyzbar-x

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),
        polygon=[
            Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
            Point(x=361, y=550)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=77,
    )
    Decoded(
        data=b'Rana temporaria', type='CODE128',
        rect=Rect(left=4, top=0, width=390, height=76),
        polygon=[
            Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
            Point(x=394, y=0)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=77,
    )
]

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),
        polygon=[
            Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
            Point(x=361, y=550)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=77,
    )
    Decoded(
        data=b'Rana temporaria', type='CODE128',
        rect=Rect(left=4, top=0, width=390, height=76),
        polygon=[
            Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
            Point(x=394, y=0)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=77,
    )
]

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),
        polygon=[
            Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
            Point(x=361, y=550)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=77,
    )
    Decoded(
        data=b'Rana temporaria', type='CODE128',
        rect=Rect(left=4, top=0, width=390, height=76),
        polygon=[
            Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
            Point(x=394, y=0)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=77,
    )
]

>>> # 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),
        polygon=[
            Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
            Point(x=361, y=550)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=77,
    )
    Decoded(
        data=b'Rana temporaria', type='CODE128',
        rect=Rect(left=4, top=0, width=390, height=76),
        polygon=[
            Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
            Point(x=394, y=0)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=77,
    )
]

>>> # 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),
        polygon=[
            Point(x=27, y=27), Point(x=27, y=172), Point(x=172, y=172),
            Point(x=172, y=27)
        ],
        orientation=<ZBarOrientation.UP: 0>,
        quality=1,
    )
]


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

Bounding boxes and polygons

The blue and pink boxes show rect and polygon, respectively, for barcodes in pyzbar/tests/qrcode.png (see bounding_box_and_polygon.py).

Two barcodes with bounding boxes and polygons

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.

Contributors

  • Alex (@globophobe) - first implementation of barcode locations

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

pyzbar_x-0.2.1-py2.py3-none-win_amd64.whl (811.6 kB view details)

Uploaded Python 2 Python 3 Windows x86-64

pyzbar_x-0.2.1-py2.py3-none-win32.whl (804.9 kB view details)

Uploaded Python 2 Python 3 Windows x86

pyzbar_x-0.2.1-py2.py3-none-any.whl (26.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyzbar_x-0.2.1-py2.py3-none-win_amd64.whl.

File metadata

  • Download URL: pyzbar_x-0.2.1-py2.py3-none-win_amd64.whl
  • Upload date:
  • Size: 811.6 kB
  • Tags: Python 2, Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for pyzbar_x-0.2.1-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 bafba142cf2f80b0931298ffe57c3c7c06dbbdf2c51a41534ec605c5cd378679
MD5 c586f89418ca88c0db1512873c16f79d
BLAKE2b-256 8aa7464a21464dfb0c718e8b7ec6dbb5e59abba498c41d048022a9bd30b8cccd

See more details on using hashes here.

File details

Details for the file pyzbar_x-0.2.1-py2.py3-none-win32.whl.

File metadata

  • Download URL: pyzbar_x-0.2.1-py2.py3-none-win32.whl
  • Upload date:
  • Size: 804.9 kB
  • Tags: Python 2, Python 3, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for pyzbar_x-0.2.1-py2.py3-none-win32.whl
Algorithm Hash digest
SHA256 6eda2f05046e84b71166ff1c85bb65f4491ae37dd4dbdafcbcd151d1821a0960
MD5 f42cf350cc77c28252cbeb1a4cbb151a
BLAKE2b-256 bffa476e0adad393c6e28a5056dffb88711a8e03f6b866a92b240e3186689b52

See more details on using hashes here.

File details

Details for the file pyzbar_x-0.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pyzbar_x-0.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for pyzbar_x-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 94f786f5bd22b5a91303c0e414edb7de58ed8f92ba847298d90d67d4073a6688
MD5 9823192d50091d6860f5c2f117631131
BLAKE2b-256 f28d1ab542ca6a56334c010f7ac4b85d431339bc0d8ee8cbba86099449b59a49

See more details on using hashes here.

Supported by

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