QR Code Generator with Python | Hivitto
What is a QR code?
QR codes are machine-readable two-dimensional pixelated barcodes that can store a variety of information. QR in QR code stands for Quick Response.
QR code was invented by a Japanese engineer Masahiro Hara from automobile manufacturer Denso Wave in the year 1994 to track the movement of car parts.
QR Code has increased in popularity in the later 2010s with improvement in optical capabilities of mobile phones and their wide adoption.
Nowadays, QR codes are being used for a wide variety of applications like making online payments, checking hotel menus, sharing wifi passwords, obtaining prices and other details of products, etc. QR Codes have become so popular that now every new smartphone comes with an inbuilt QR code reader.
In this article, we will learn how to read and generate QR Codes using python.
Generate QR Code
Install QR Code module
We will be using the QRcode package for generating QR codes.
The first step is installing the package using the pip command.
pip install QRcode
Full documentation of the package can be accessed on the PyPI homepage of the package.
Simple QR Code
A simple QR code can be generated by using the make function of QRcode and passing the data as an argument.
The below code produces a QR code that reads ‘Hello World.’
#Import Library
import QRcode#Generate QR Code
img=qrcode.make('Hello World')
img.save('hello.png')

You can use your smartphone to read the above code.
Caution: Do not use your phone to read random QR codes as they may contain malicious code/links.
Advanced QR Code
QR code can be customized using the QRCode object which has the following parameters:
i. version:
There are 40 versions of QR codes that control the code's size.
1 being the smallest and 40 being the largest.
Version 1 will create a 21X21 matrix QR Code.
ii. error_correction:
This parameter controls the Error Correction used for the QR code. This varies from 7% to 30% error correction as below.
ERROR_CORRECT_L: up to 7%
ERROR_CORRECT_M: up to 15%
ERROR_CORRECT_Q: up to 25%
ERROR_CORRECT_H: up to 30%
iii. box_size:
This parameter controls the number of pixels in each box of the QR code
iv. border:
This parameter controls the thickness of the border. The default border is 4 pixels thick.
The QRCode object has the following functions which can be used to create the QR Code.
i. add data
The content of the QR code can be passed as an argument to this function.
ii. make
If you are not sure about which version of QR code to use, the version can be set automatically by :
a. setting version parameter to None and
b. setting fit parameter of make to True.
iii. make image
This function generates the QR code. It can also be used to set the fill color and background color of the QR code using fill_color and back_color arguments.
The following code generates a QR code which points towards my medium profile.
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data("https://abhijithchandradas.medium.com/")
qr.make(fit=True)img = qr.make_image(fill_color="red", back_color="black")
img.save("medium.png")

Reading QR Code
We will use OpenCV to read the QR codes. If the package is not installed, it can be installed as below:
pip install cv2
QR code can be decoded using detectAndDecode function of QRCodeDetector object of OpenCV.
import cv2img=cv2.imread("medium.png")
det=cv2.QRCodeDetector()
val, pts, st_code=det.detectAndDecode(img)
print(val)Output:
https://abhijithchandradas.medium.com/
The detect and decode function returns the content of the QR code, coordinates of the corners of the box, and binarized QR code.
You can refer OpenCV QRCodeDetector class reference for more information on reading QR codes using OpenCV.