What is QR code?
QR codes are machine readable two dimensional pixelated barcodes which can be used to 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 wide variety of applications like, make online payments, check hotel menu, share wifi password, obtain price and other details of products etc. QR Codes have become so popular that now every new smartphone comes with in built QR code reader.
In this article we will learn how to read and generate QR Code using python.
Generate QR Code
Install QR Code moduleWe will be using qrcode package for generating QR code. The first step is installing the package using pip command.
pip install qrcode
Full documentation of the package can be accessed in 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 argument. The below code produces a QR code which reads ‘Hello World.’
#Import Library
import qrcode
#Generate QR Code
img=qrcode.make('Hello World')
img.save('hello.png')

You can use your smart phone to read the above code.
Caution: Do not use your phone to read random QR codes as it may contain malicious code/links.
Advanced QR Code
QR code can be customized using QRCode object __ which has the following parameters:
i. version: There are 40 versions of QR code which controls the size of the code. 1 being the smallest and 40 being the largest. Version 1 will create a 21X21 matrix QR Code.
_ii. errorcorrection: 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. boxsize: 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. seting 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 _fillcolor and _backcolor 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 QR code. 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 cv2
img=cv2.imread("medium.png")
det=cv2.QRCodeDetector()
val, pts, st_code=det.detectAndDecode(img)
print(val)
Output:
https://abhijithchandradas.medium.com/
The detectAndDecode 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 code using OpenCV.
Resources:
Code for the tutorial is available in my GitHub Repo.
Become a Member
I hope you like the article, I would highly recommend signing up for Medium Membership to read more articles by me or stories by thousands of other authors on variety of topics. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.
Related Article:
