用python从左到右解码qr码

2024-06-28 20:20:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个png和几个qr码,基本上是这样的

enter image description here

我用zbarlight来解码qr码。在

from PIL import Image
import zbarlight

file_path = './tests/qr_codes.png'
with open(file_path, 'rb') as image_file:
    image = Image.open(image_file)
    image.load()

codes = zbarlight.scan_codes(['qrcode'], image)
print('QR codes: %s' % codes)

我的目标是从左到右解码qr码,所以列表应该是这样的:url1、url2、url3、url4、url5、ulr6。在

问题:在我看来,zbarlight扫描过程的结果(列表)像是随机顺序。有没有办法从左向右扫描?在


Tags: pathfromimageimport列表pilpngtests
1条回答
网友
1楼 · 发布于 2024-06-28 20:20:40

我在windows上,现在还没有办法在linux上测试,但是这看起来像预期的那样工作。在

import sys, os
try:
    from pyzbar.pyzbar import decode, ZBarSymbol
except:
    cmd = ('py -m pip install "pyzbar"')
    os.system(cmd)
    from pyzbar.pyzbar import decode, ZBarSymbol

try:
    from PIL import Image
except:
    cmd = ('py -m pip install "Pillow"')
    os.system(cmd)
    from PIL import Image

decoded = decode(Image.open("C:/Temp/13AZQ.png"), symbols=[ZBarSymbol.QRCODE])
qr_dic = {}
for qr in decoded:
    x = qr[2][0] # The Left position of the QR code
    qr_dic[x] = qr[0] # The Data stored in the QR code

for qr in sorted(qr_dic.keys()):
    print(qr_dic[qr])

输出:

^{pr2}$

相关问题 更多 >