Naik
Naik

Reputation: 1255

Decode QR using pyzbar or pylibdmtx

I have been working to decode some QR codes from images in Python3 using pyzbar or pylibdmtx, however, the codes do not work in most cases and return an empty list. Here are my codes:

# import the required packages
import cv2
from pyzbar import pyzbar
from pylibdmtx import pylibdmtx
from PIL import Image

# reads image 
img = cv2.imread('test1.png') # see the link for the image below

You can see the image I used for this example here enter image description here. Even I cropped the image and separated the QR code (it still is not working).

Then, I used pyzbar package as follows:

codes = pyzbar.decode(img)

It returns an empty list. I also tried to use pylibdmtx and got an empty list.

codes = pylibdmtx.decode(cv2.imread('test1.png'))

Here is the link for the sample QR code. Please note that using QR code reader on my phone, I was able to decode this QR code.

I also tried the solution mentioned in other posts like binarization as mentioned here {https://stackoverflow.com/questions/61442775/preprocessing-images-for-qr-detection-in-python}

from kraken import binarization
bw_im = binarization.nlbin(img)
# zbar
pyzbar.decode(bw_im, symbols=[pyzbar.ZBarSymbol.QRCODE])

Upvotes: 3

Views: 3676

Answers (1)

Rotem
Rotem

Reputation: 32094

The image you have posted is not a QR code but an Aztec Code.

  • A QR code has distinctive squares at the corners.
  • An Aztec Code has a "square grid with a bulls-eye pattern at its center".

For decoding Aztec Code we may use python-zxing.
Installation: pip install zxing.

Sample code for decoding Aztec Code:

import zxing

reader = zxing.BarCodeReader()
barcode = reader.decode("test1.png")
print(barcode)

Output:

BarCode(raw='M1LOPEZ/DANITZA K9S3KG MEXCULY4 0792 047Y004A0067 147>1182 M8046BY4 0000000000000290360000000004 0 ', parsed='M1LOPEZ/DANITZA K9S3KG MEXCULY4 0792 047Y004A0067 147>1182 M8046BY4 0000000000000290360000000004 0 '...

Upvotes: 5

Related Questions