使用Zbar确定二维码角位置的Python代码

5 投票
1 回答
5124 浏览
提问于 2025-04-18 07:15

我想在这里扩展一下代码,以提取二维码的角落。

#!/usr/bin/python
from sys import argv
import zbar
from PIL import Image

if len(argv) < 2: exit(1)

# create a reader
scanner = zbar.ImageScanner()

# configure the reader
scanner.parse_config('enable')

# obtain image data
pil = Image.open(argv[1]).convert('L')
width, height = pil.size
raw = pil.tostring()

# wrap image data
image = zbar.Image(width, height, 'Y800', raw)

# scan the image for barcodes
scanner.scan(image)

# extract results
for symbol in image.symbols:
    # do something useful with results
    print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data

# clean up
del(image)

在之前的一篇帖子中,作者提到这是可能的,“*zBar提供了一种方法,可以根据像素值来做到这一点(一旦你知道像素值的大小,就可以用百分比找到它)。”

可以查看 使用OpenCV和Zbar在Python中检测二维码的大小

我参考了Zbar的开发工具包(http://zbar.sourceforge.net/api/classzbar_1_1Symbol.html),但仍然不知道如何提取角落。如果有人能教我怎么用Python提取角落,我将非常感激。

1 个回答

5

在你的循环中,试试这个:

topLeftCorners, bottomLeftCorners, bottomRightCorners, topRightCorners = [item for item in symbol.location]

这样你就能得到四个角落的坐标了

虽然回答得晚了,但希望这能帮助到其他人。

Dom

撰写回答