Archie  Clayton

Archie Clayton


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

QR code is a type of matrix barcode that is machine readable optical label that contains information about the item to which it is attached. In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application.

Installing required dependencies:

pip3 install opencv-python qrco

Generate QR Code

It is basically straight forward to generate QR code using qrcode library:

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

This will generate a new file in the current directory with the name of “site.png”, which contains a QR code image of the data specified, easy enough?

Read QR Code

There are many tools that reads QR code. However, we will be using OpenCV for that, as it is popular and easy to integrate with the webcam. Alright, open up a new Python file and follow along with me:

Let’s read the image that is just generated:

import cv2
# read the QRCODE image
img = cv2.imread("site.png")

Luckily for us, OpenCV already got QR code detector built in:

# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()

We have the image and the detector, let’s detect and decode that data:

# detect and decode
data, bbox, straight_qrcode = detector.detectAndDecode(img)

detectAndDecode() function takes an image as an input and returns a tuple of 3 values: the data decoded from the QR code, the output array of vertices of the found QR code quadrangle and the output image containing rectified and binarized QR code.

We just need data and bbox here, bbox will help us draw the quadrangle in the image and data will be printed to the console!

Let’s do it:

# if there is a QR code
if bbox is not None:
    print(f"QRCode data:\n{data}")
    # display the image with lines
    # length of bounding box
    n_lines = len(bbox)
    for i in range(n_lines):
        # draw all lines
        point1 = tuple(bbox[i][0])
        point2 = tuple(bbox[(i+1) % n_lines][0])
        cv2.line(img, point1, point2, color=(255, 0, 0), thickness=2)

cv2.line() function draws a line segment connecting two points, we retrieve these points from bbox array that was decoded by detectAndDecode() previously. we specified a blue color ( (255, 0, 0) is blue as OpenCV uses BGR colors ) and thickness of 2.

Finally, let’s show the image and quit when a key is pressed:

# display the result
cv2.imshow("img", img)

We are done with this script, try to run it and see your own results!

If you want to detect and decode QR codes live using your webcam (and I’m sure you do), here is a code for that:

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"):

Awesome, we are done with this tutorial, you can now integrate this in your own applications!

Check the full code here.

Happy Coding ♥

#python #opencv #machine-learning

What is GEEK

Buddha Community

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

Ray Patel


top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel


Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Verda  Conroy

Verda Conroy


Create a Virtual Pen and Eraser with Python OpenCV - Genial Code

Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it’s like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.

#python #create virtual pen and eraser with opencv #create virtual pen and eraser with python opencv #programming #opencv #python opencv

Python Imread(): Different Ways to Load an Image using The OpenCV.imread() Method

In this tutorial, we will learn how to use imread() method of OpenCV-Python in detail and different ways to load an image using imread() method.

Table of Contents

What is Python imread()?

Importing OpenCV to use Python imread()

Syntax of the Python imread() method

Image formats supported by Python imread() method

#python modules #opencv-python #python imread() #opencv.imread() #python imread(): different ways to load an image using the opencv.imread() method #load an image

Ray  Patel

Ray Patel


Using Black To Auto Format Your Python

Writing python code is one thing and writing the code in a good format is another thing. Most of the time especially junior coders/programmers focus on making sure the codes are working and forget to format their codes.

If you write a small program (with 1000 lines of codes) you could get away without formatting your code, but as programs get more and more complex, they get harder and harder to understand and at some point (around 15,000 lines of code), it becomes harder to understand the code that you yourself wrote.

The difference between working on well-formatted code and working on badly formatted code is like the difference between living in a palace and living in a dirty house.

Why formatting your python code is important?

#programming #python #python-programming #python-tutorials #python-tips #python-developers #code-quality #clean-code