Decode qr codes with python from left to right

有些话、适合烂在心里 提交于 2019-12-11 04:14:45

问题


I have a png with several qr codes which basically looks like this

To decode the qr codes I use zbarlight.

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)

My goal is to decode the qr codes FROM LEFT TO RIGHT, so the list should like like this: url1, url2, url3, url4, url5, ulr6.

Problem: The result (list) of the zbarlight scanning process looks to me like a random order. Is there a way to scan FROM LEFT TO RIGHT?


回答1:


I'm on windows, no way of testing on linux right now, but this appears to work as expected.

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

Output:

b'url1'
b'url2'
b'url3'
b'url4'
b'url5'


来源:https://stackoverflow.com/questions/51195584/decode-qr-codes-with-python-from-left-to-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!