NaturalHistoryMuseum/pyzbar

Name: pyzbar

Owner: Natural History Museum

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

Created: 2016-11-08 08:54:38.0

Updated: 2018-05-20 12:05:40.0

Pushed: 2018-05-13 12:27:15.0

Homepage:

Size: 90

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

pyzbar

Python Versions PyPI version Travis status Coverage Status

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

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:

 install zbar

Linux:

 apt-get install libzbar0

Install this Python wrapper; use the second form to install dependencies of the command-line scripts:

install pyzbar
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))
eback (most recent call last):
le "<stdin>", line 1, in <module>
le "/Users/lawh/projects/pyzbar/pyzbar/pyzbar.py", line 102, in decode
raise PyZbarError('Unsupported bits-per-pixel [{0}]'.format(bpp))
ar.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

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


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.