Skip to main content

Python Versions PyPI version Travis status Coverage Status

Read one-dimensional barcodes and QR codes from Python 2 and 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 2.7, and Python 3.4 to 3.6

The older zbar package is stuck in Python 2.x-land. The zbarlight package doesn’t 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; 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),
        polygon=[
            Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
            Point(x=361, y=550)
        ]
    )
    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)
        ]
    )
]

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

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

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

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


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

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).

Release files for pyzbar 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for pyzbar 0.1.7
File Interpreter ABI Platform
pyzbar-0.1.7-py2.py3-none-win_amd64.whl Python 3, Python 2 none Windows x86-64 Details
pyzbar-0.1.7-py2.py3-none-win32.whl Python 3, Python 2 none Windows x86-32 Details
pyzbar-0.1.7-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size:1.6 MB

Release files / pyzbar-0.1.7-py2.py3-none-win_amd64.whl

Download URL pyzbar-0.1.7-py2.py3-none-win_amd64.whl
Size 812.6 kB
Tags Python 2 Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
98ee05a856265b37fc2a135d3d51eca5e753193e3d22e74c482c963e2d1c22d4
BLAKE2b-256 checksum
How to use checksums
131a9ace0828241ec9183b840c4de6f49b7490a152414c3f824bab422649e824
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pyzbar-0.1.7-py2.py3-none-win32.whl

Download URL pyzbar-0.1.7-py2.py3-none-win32.whl
Size 805.9 kB
Tags Python 2 Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
16df691a5fa82cb35216508ec5ee32bc469d5f37dca7523d5bbad0271ba13c2c
BLAKE2b-256 checksum
How to use checksums
3852a20f19ce0d829c609c709a84dd2aebcb91c23f55f9944aa492e0fa266c78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pyzbar-0.1.7-py2.py3-none-any.whl

Download URL pyzbar-0.1.7-py2.py3-none-any.whl
Size 27.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
60f1e5871f2fff5ea38b2fa5dd2e7060eed828d0e0d937b6c0130a5052aa2a3d
BLAKE2b-256 checksum
How to use checksums
bde7dc9aa3ee5ddc71df6ea1698f7e9d12dcc874346ad3051524155e121006a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.1.9

3 release files

0.1.8

3 release files

This release

0.1.7 This release

3 release files

0.1.5

3 release files

0.1.4

3 release files

0.1.3

3 release files

0.1.2

3 release files

0.1.1

3 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page