NaturalHistoryMuseum / pyzbar

Read one-dimensional barcodes and QR codes from Python 2 and 3.
MIT License
726 stars 176 forks source link

Can't get QR to be decoded #134

Open miqmago opened 2 years ago

miqmago commented 2 years ago

Thanks for this awesome library!

I've made a simple script to decode qr. In general it works fine but there are some qr that it can't get to decode. Is there any parameters that I could adjust or something I'm missing?

version: v0.1.8

#! /usr/local/bin/python3.9

import numpy as np
from argparse import ArgumentParser
from PIL import Image
from pyzbar.pyzbar import decode, ZBarSymbol

parser = ArgumentParser()
parser.add_argument('-f', '--file', type=str)
parser.add_argument('-d', '--debug', default=False, action='store_true')
args = parser.parse_args()

thresh = 127
maxval = 255

def find_qr(img: Image, page):
    imgArr = np.array(img)
    imgBin = np.uint8((imgArr > thresh) * maxval)

    if args.debug:
        Image.fromarray(imgBin).save('img-%d.png' % page)
        # uncomment to skip binarization
        # imgBin = img
        # imgBin.save('img-%d.png' % page)

    data = decode(imgBin, symbols=[ZBarSymbol.QRCODE])
    if not data is None and len(data) > 0 and data[0].type == 'QRCODE':
        print(data[0].data.decode(), end='')
        return True

    return False

find_qr(Image.open(args.file).convert('L'), 0)

Original image: img-0

Binarization of the image:

img-0

I've tried to decode without binarization and binarizing with higher and lower thresholds but never get's decoded.

I've tried several online decoders which can decode original image as well as binarized:

ronytesler commented 1 year ago

I also try to decode images with some damaged modules and it doesn't work. The phone's reader can read them successfully. Any solution for this?