Decode Qr Code with 6 Lines of Python Code

Decode QR Code with 6 lines of Python code

Decoding a QR Code to a text-based output can be a hard job unless you try out the logic we’ve used to decode QR Code with 6 lines.

In our previous article, we used python to Generate a QR Code with less than 3 lines of code. An image was generated as an output of the program containing the encoded QR Code.

Decode QR Code with 6 lines of Python code

Though a modern mobile phone camera will be enough to decode a QRCode, this python program delivers an idea to decode QR codes in an exceptional way. Of course, you can tweak the program to achieve your desired performance.

Prerequisites

The previous article generated a QR Code that is inside the Present working directory which will be eventually used as the input of the current program.

We will use the ‘OpenCV (opens in a new tab)‘ computer vision library with python to recognize and decode the QR Code.

As we are proceeding towards installing a python library, I guess you already have Python installed on your computer. If not, then Install it from Python.org (opens in a new tab).

You can install OpenCV easily from your Command prompt terminal. Click the Win key and Search by typing CMD, then press Enter.

Type the following command, and press Enter. Wait for it to Download, and get installed on its own.

 pip install opencv-python
 

Decoding QR Code

After you have installed OpenCV installed it’s time to hit the road towards creating our QR decoding snippet.

You can either have python Globally installed or have it either by creating a virtual environment. For our tutorial, it’s installed globally and we’ll simply create a project through VSCode (opens in a new tab) which is created by Microsoft and is an awesome open-source coding environment.

Open up VSCode and create a new file name ‘Decoder.py’, copy the below-provided snippet, and paste it inside the file.

 # Importing the OpenCV Library
 
import cv2
 
# initialize the QRCode detector
 
decoder = cv2.QRCodeDetector()
 
# Name of the QR Code Image file
 
file_name = "myQr.jpg"
 
# read the QRCODE image
 
image = cv2.imread(file_name)
 
# detect and decode
 
link, data_points, straight_qrcode = decoder.detectAndDecode(image)
 
# Print the outcome
 
print(link)

The code is written in a way that explains the flow of task execution by commented instructions.

Make sure you have the previously generated QR Code or any other QR Code image inside the same folder where you have created the ‘decoder.py’ file.

You can copy the file name along with the image type and change it inside the code snippet where it reads the image ‘myQr.jpg’. Your image name may be ‘Qr_4’ and it’s of ‘png’ type, in that case, change the code to ‘file_name = “Qr_4.png” and you will be good to go.

Conclusion

For obvious reasons we’ve kept the code beginner-friendly, It can be tweaked and shortened further to 3-4 lines of code.

Previously we’ve composed an article explaining ‘how to generate QR Code with less than 3 lines of python code’, this was an extension of that article explaining how to Decode QR Code with 6 lines of Python code.

I hope this tutorial was helpful to you, if you’re having any trouble don’t mind commentting down.

IndGeek provides solutions in the software field, and is a hub for ultimate Tech Knowledge.