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.
You can use pyzbar
From their docs:
from pyzbar.pyzbar import decode from PIL import Image decode(Image.open('pyzbar/tests/code128.png'))
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With