从Python 2和3中读取一维条码和QR码。
项目描述
使用zbar库从Python 2和3中读取一维条码和QR码。
纯Python
支持PIL / Pillow图像、OpenCV / imageio / numpy ndarray和原始字节
解码条码位置
除了zbar库本身外,没有其他依赖项
已在Python 2.7和Python 3.5到3.10上测试
较老的zbar软件包卡在Python 2.x-land。 zbarlight软件包不提供Windows支持并依赖于Pillow。
安装
Windows Python轮盘包含了zbar DLLs。在其他操作系统上,您需要安装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) ], orientation="UP", 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="UP", quality=77 ) ]
它还接受来自OpenCV加载图像的numpy.ndarray实例。
>>> 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="UP", 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="UP", quality=77 ) ]
您还可以提供一个元组(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) ], orientation="UP", 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="UP", 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="UP", 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="UP", 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]
默认行为是解码所有符号类型。您可以只查找您的符号类型
>>> 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="UP", 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]) []
ZBar 版本
原始 zbar 的开发于 2012 年停止。2019 年在 新项目 下重新开始开发,该项目增加了一些新功能,包括解码条码方向的支持。撰写本文时,该项目不生成 Windows DLL。Windows Python 轮子中包含的 zbar DLL 是从原始项目构建的,因此不支持解码条码方向。如果您看到 orientation=None,则表示您的系统安装的是不支持方向的旧版 zbar。
质量字段
从 zbar.h,质量字段是
…一个未缩放的相对量:较大的值比较小的值好,其中“大”和“小”取决于应用程序。预期这个量的确切定义会随着指标细化而变化。目前,只定义了两个值之间的有序关系,并将在未来保持稳定
边界框和多边形
蓝色和粉红色的框分别显示了 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;如果使用 32 位 Python,请安装 vcredist_x86.exe。
贡献者
Alex (@globophobe) - 首次实现条码位置
Dmytro Ferens (@dferens) - 条码方向
Ismail Bento (@isman7) - 支持使用 imageio 加载的图像
@jaant - 读取包含空字符的条码
许可证
pyzbar 在 MIT 许可证下发布(参见 LICENCE.txt)。zbar 共享库在 GNU Lesser General Public License,版本 2.1 下发布
项目详情
下载文件
下载您平台上的文件。如果您不确定该选择哪个,请了解更多关于 安装软件包 的信息。