阅读Python2和3中的一维条码和二维码。
pyzbar的Python项目详细描述
使用 zbar库。
- 纯Python
- 适用于pil/枕头图像、opencv/numpyndarrays和原始字节
- 解码条形码的位置
- 除了zbar库本身之外,没有依赖项
- 在Python2.7和Python3.4到3.6上测试
旧的zbar 包被困在python 2.x-land中。 zbarlight包没有 靠枕头支撑窗户。
安装
zbarDLLs包含在windows python控制盘中。 在其他操作系统上,您需要安装zbar共享 图书馆。
Mac OS X:
brew install zbar
Linux:
sudo apt-get install libzbar0
安装这个python包装器;使用第二个表单安装 命令行脚本:
pip install pyzbar pip install pyzbar[scripts]
示例用法
decode函数接受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) ] ) ]
它还接受numpy.ndarray的实例,这些实例可能来自加载 使用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) ] ) ]
您还可以提供一个元组(pixels, width, height),其中图像数据 是每像素8位。
>>> 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]
默认行为是解码所有符号类型。你可以找你的 符号类型
>>> 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]) []
边界框和多边形
蓝色和粉色框分别显示rect和polygon,用于 pyzbar/tests/qrcode.png中的条形码(请参见 bounding_box_and_polygon.py)。
Windows错误消息
如果在windows上导入pyzbar时看到丑陋的ImportError。 您很可能需要Visual C++ Redistributable Packages for Visual Studio 2013。 如果使用64位python,请安装vcredist_x64.exe,如果使用vcredist_x86.exe。 使用32位python。
贡献者
- alex(@globophobe)-条码位置的首次实现
许可证
pyzbar在mit许可证下分发(请参见LICENCE.txt)。 zbar共享库分布在gnu lesser general下 公共许可证,版本2.1(请参见zbar-LICENCE.txt)。