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

pyzbar-upright的Python项目详细描述


https://img.shields.io/badge/python-2.7%2C%203.4%2C%203.5%2C%203.6-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/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])
[]

边界框和多边形

蓝色和粉色框分别显示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.exeif,vcredist_x86.exeif 使用32位Python。在

贡献者

  • Alex(@globophobe)-首次实现条形码位置

许可证

pyzbar是根据麻省理工学院的许可证分发的(参见LICENCE.txt)。 zbar共享库是在GNU Lesser General下分发的 公共许可证,版本2.1(参见zbar-LICENCE.txt)。在

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

推荐PyPI第三方库


热门话题
java使图像以正确的速度在屏幕上移动,以适应所有显示   内存Java分配:从预先存在/分配的池中分配对象   java这种书写方式?   Java正则表达式查找字符串的开头   java是否可以创建一个类来处理安卓中的所有日志代码(例如log.d(TAG,message))   如何使用Selenium和java单击WebTable任意页面上的WebElement   java解析字符串中的文件名   java刷新JTree内容   java如何覆盖RequestMappingHandler   爪哇数石头、布、剪刀赢了多少   struts中的java无效令牌   swing JTree,优化算法,Java   java Tomcat和SSL:密钥库格式无效