How to Generate and Read QR Code using Python and OpenCV?

In this article, we will see how to generate and read the QR code using Python and OpenCV?
Submitted by Abhinav Gangrade, on July 12, 2020

Modules Used:

In this article, we will use two modules qrcode and opencv(cv2).

qrcode Module:
qrcode is a Python library that will help us to generate QR code.

We can install it as:

  • General Way: pip install qrcode
  • Pycharm Users: Go to the project interpreter and install it from there.

python-opencv(cv2) Module:

Python-opencv(cv2) is a python library that will help us to solve the computer vision problems and in this article, we will use this module to scan the QR code.

We can install it as:

  • General Way: pip install opencv-python
  • Pycharm users: Go to the project interpreter and install it from there.

What we will do in this article?

In this article, we will generate a QRcode with data inside it and then we will read that QR code with the help of opencv.

Let's see the code for generating Qrcode:

# importing the modules
import qrcode

# enter the data
Data="https://www.google.com"

# create a filename
filename="Qrcode.png"

# generate the qrcode
image=qrcode.make(Data)

# save the image
# the image will be saved in
# the same directory
# you can also give a path
image.save(filename)

Output:

Generate and Read QR Code using Python and OpenCV

Python code for Scanning the Qrcode with opencv

# importing the modules
import cv2

# like we have saved the qrcode
# so we will read that image
image=cv2.imread("Qrcode.png")

# create a qrcode detector
detect=cv2.QRCodeDetector()

# get the data and other threshold
# put the image that we have read
data,bbox,sqrcode=detect.detectAndDecode(image)

# bbox is the main thing in the qrcode
# if it exist it will give us the data
if bbox is not None:
    print(data)

Output:

As we have given a link in the data when we created the QRcode so that data is printed on our consoles,

https://www.google.com


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.