阅读Python2和3中的一维条码和二维码。

pyzbar的Python项目详细描述


https://img.shields.io/badge/python-2.7%2C%203.4%2C%203.5%2C%203.6%2C%203.7-blue.svghttps://badge.fury.io/py/pyzbar.svghttps://travis-ci.org/NaturalHistoryMuseum/pyzbar.svg?branch=masterhttps://coveralls.io/repos/github/NaturalHistoryMuseum/pyzbar/badge.svg?branch=master

使用 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])
[]

边界框和多边形

蓝色和粉色框分别显示rectpolygon,用于 pyzbar/tests/qrcode.png中的条形码(请参见 bounding_box_and_polygon.py)。

Two barcodes with bounding boxes and polygons

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Oracle将休眠为ISO 8601日期格式   当有线程时,swing计时器不会停止。睡在Java里面   如何使用swing在java中清空密码字段值(字符串)   如何在编辑文本字段上设置单词java(安卓)   单独类中的java OkHttp请求   java Tomcat配置文件/上下文xml似乎已经崩溃了。请确保它是可分析和有效的。有关详细信息,请参阅服务器日志   java在科尔多瓦的ActivityResult上传递   java如何在映射中保持插入顺序。工厂?   “DataOutputStream”和“ObjectOutputStream”之间的java差异   java从FTP文件列表中获取项目的时间戳   java如何在spring security中为每个人忽略一些资源/URL?   模板类嵌套时新的Java泛型类构造函数问题   java读取并查找文件大小为1GB的行   java如何使用字符串say“stop”停止整数格式的while循环   java是否可以在应用程序启动之间将JVM保留在内存中?   java Springboot出现“出现意外错误(类型=内部服务器错误,状态=500)”的问题