Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode QR Code in Python 3 [closed]

I am looking for an easy way to decode QR Codes in png format in python 3. Many of the previous answers I found, seem to only work with python 2. For example the qrtools package does not work, because zbar does not work with python 3.

It would be very helpful if someone could suggest a package to use and provide a basic example on how to decode the QR code.

like image 240
phil Avatar asked Jul 24 '26 18:07

phil


2 Answers

You can use pyzbar

From their docs:

from pyzbar.pyzbar import decode
from PIL import Image
decode(Image.open('pyzbar/tests/code128.png'))
like image 88
Nader Alexan Avatar answered Jul 26 '26 07:07

Nader Alexan


You can refer here for more detailed tutorial pyzbar + opencv for python 3

In nutshell:

from pyzbar import pyzbar
import cv2

img_path = 'image.png'

img = cv2.imread(img_path)

barcodes = pyzbar.decode(img)

for barcode in barcodes:
    (x, y, w, h) = barcode.rect
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
    barcodeData = barcode.data.decode("utf-8")
    barcodeType = barcode.type
    text = "{} ({})".format(barcodeData, barcodeType)
    cv2.putText(img, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    print("[INFO] found {} barcode {}".format(barcodeType, barcodeData))

cv2.imwrite("new_img.jpg", img)
like image 32
Akson Avatar answered Jul 26 '26 09:07

Akson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!