阅读Python2和3中的一维条形码和二维码。
pyzbar-upright的Python项目详细描述
使用 zbar库。在
- 纯Python
- 可用于PIL/Pillow图像、OpenCV/numpyndarrays和原始字节
- 解码条形码的位置
- 除了zbar库本身,没有依赖项
- 在Python2.7和Python3.4到3.6上进行了测试
年长的zbar 包被困在python2.x-land中。 zbarlight包没有 靠枕头支撑窗户。在
安装
zbarDLLs包含在Windows Python控制盘中。 在其他操作系统上,您需要安装zbarshared 图书馆。在
Mac OS X:
brew install zbar
Linux系统:
^{pr2}$安装这个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]) []
Windows错误消息
如果在Windows上导入pyzbar时看到一个难看的ImportError 您很可能需要Visual C++ Redistributable Packages for Visual Studio 2013。 使用64位Python安装vcredist_x64.exeif,vcredist_x86.exeif 使用32位Python。在
贡献者
- Alex(@globophobe)-首次实现条形码位置
许可证
pyzbar是根据麻省理工学院的许可证分发的(参见LICENCE.txt)。 zbar共享库是在GNU Lesser General下分发的 公共许可证,版本2.1(参见zbar-LICENCE.txt)。在
- 项目
标签: