Python 3 (OpenCV + Numpy + Qrcode) Example Script to Scan or Read Qr Codes and Generate QR Codes Full Example Project For Beginners

Python 3 (OpenCV + Numpy + Qrcode) Example Script to Scan or Read Qr Codes and Generate QR Codes Full Example Project For Beginners


Welcome folks today in this post we will be scanning or reading  qr codes and also generating qr codes in python using opencv and numpy and qrcode library. All the source code of the application is given below.




Get Started




In order to get started you need to install the following libraries


pip install qrcode


pip install opencv-python


pip install numpy


After installing all these libraries we need to create an file and copy paste the following code


First of all we will be generating simple qr codes using qrcode library and numpy


import qrcode
# example data
data = ""
# output file name
filename = "codingshiksha.png"
# generate qr code
img = qrcode.make(data)
# save img to a file



Simply we are importing the qrcode library and then we are generating the simple qr code containing the website url and we have given the output file name as codingshiksha.png so when you run the python script as shown below




READ  Python Script to Convert CSV File to HTML Table Using PrettyTable Library Full Tutorial For Beginners

You will see the qr code image being created as shown below








You can even make colored qr codes having custom border as well as shown below in the example


import qrcode
import numpy as np
# data to encode
data = ""
# instantiate QRCode object
qr = qrcode.QRCode(version=1, box_size=10, border=4)
# add data to the QR code
# compile the data into a QR code array
# print the image shape
print("The shape of the QR image:", np.array(qr.get_matrix()).shape)
# transfer the array into an actual image
img = qr.make_image(fill_color="white", back_color="red")
# save it to a file"codingshiksha.png")



Now this will be a red colored qr code as shown below when you run it





Reading QR Codes



Now we will look at how to read the data which is contained inside the qr codes as shown below in the example


import cv2
# initalize the cam
cap = cv2.VideoCapture(0)
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()
while True:
    _, img =
    # detect and decode
    data, bbox, _ = detector.detectAndDecode(img)
    # check if there is a QRCode in the image
    if bbox is not None:
        # display the image with lines
        for i in range(len(bbox)):
            # draw all lines
            cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255, 0, 0), thickness=2)
        if data:
            print("[+] QR Code detected, data:", data)
    # display the result
    cv2.imshow("img", img)    
    if cv2.waitKey(1) == ord("q"):


READ  Python 3 Script to Check PC or Laptop OS Last Reboot Time Using psutil Library Full Project For Beginners


Here we are using the python-opencv library to read the data of qr code as shown below








Leave a Reply