Reading QR code in Python

We learned how to create QR code in last blog. Now, let's see how we can read these QR code using python. 

Package Required:
    pip install opencv-python

We can use below code to read a QR code image. 

import cv2

det=cv2.QRCodeDetector()
img_1 = cv2.imread("sample_1.png")
val_1, _, _ = det.detectAndDecode(img_1)
print("Sample_1 Data =", val_1)

if image is not in gray or RGB. it might not able to decode, For example I created a QR code with fill color as yellow and back color as black and it couldn't decode this code. One more step I tried to read this image in gray scale, picture was not very clear. May be due to this it couldn't decode. 
After changing fill color as purple, green or red and back color as white, message was decoded successfully. 

In order to read image in gray scale and save using cv2, we can use below code. 

img_2 = cv2.imread("sample_2.png", 0)
cv2.imwrite('gray_sample2.png', img_2)

Thank you!!!

Comments

Popular posts from this blog

\r issue in shell script

Create QR code using qrcode in python

Git Command