top of page
Writer's picturerajendra gupta

Generate and Decode QR Codes with Python

This post discusses how to use Python to generate and decode QR Codes. Learn how to use Python to develop your custom QR Code.


The QR code is a machine-readable optical label containing the attached data. This QR code is popular for identifiers, trackers, and locators. You might be familiar with the QR code with the increasing digital payments that scan the QR code to fill sender or receiver information automatically.


Recently I thought of generating a QR code for my website https://www.dbblogger.com so that users do not need to enter the website name in the browser for navigation. There are different ways to generate the QR Code. However, I explored Python for generating QR codes and decoding QR codes for retrieving associated data.


Azure Data Studio embeds an SQL notebook that is an executable notebook with different kernels such as Python, SQL, Scala R, PySpark, and PowerShell.

Launch Azure Data Studio and click on Create a notebook.


Select the Python 3 kernel from the drop-do

wn

We require the following Python packages to work with Python for this article.

  • OpenCV-python: This library is useful for computer vision, image processing, and machine learning. Its in-built QRCodeDetector class helps decode QR codes.

  • QRcode: The QRcode python package helps generate and read QR codes.

  • NumPy: Numpy is useful for mathematical and logical operations on multidimensional array objects.

  • Image: The Image module helps work with images in Python.





Click on Manage Packages in Azure Data Studio, search for the required package and install it.




Generate a QR code for the website https://www.dbblogger.com

The following Python code imports the QRcode module and generates a QR code with the qrcode.make() function. The function requires the input of data for which we require a QR code.

The QR code is saved into a directory as per the parameter qrcodefilename


import qrcode
qrcodedata = "https://www.dbblogger.com"
qrcodefilename = "c://temp//mywebsite.png"
img = qrcode.make(qrcodedata)
img.save(qrcodefilename)

As shown below, the below QR code is generated for https://www.dbblogger.com











The QR code represents a black line fill and white background by default. We can customize the QR code using Python as well. For example, let's fill the QR code with RED color instead of black.

For filling the color, we use make_image() function and specify filling color with fill_color as shown below.

import qrcode
qrcodedata = "https://www.dbblogger.com"
qrcodefilename = "c://temp//mywebsite.png"
qrObject = qrcode.QRCode()
qrObject.add_data(qrcodedata)
qrObject.make()
qrimage = qrObject.make_image(fill_color="red")
qrimage.save(qrcodefilename)












Simiarly, the following Python code generates QR code filled with blue color.


import qrcode
qrcodedata = "https://www.dbblogger.com"
qrcodefilename = "c://temp//mywebsite.png"
qrObject = qrcode.QRCode()
qrObject.add_data(qrcodedata)
qrObject.make()
qrimage = qrObject.make_image(fill_color="blue")
qrimage.save(qrcodefilename)










We can specify the background color using the back_color parameter. As shown below, the QR code background is yellow with a black (default) filling color.


import qrcode
qrcodedata = "https://www.dbblogger.com"
qrcodefilename = "c://temp//mywebsite.png"
qrObject = qrcode.QRCode()
qrObject.add_data(qrcodedata)
qrObject.make()
qrimage = qrObject.make_image(back_color="yellow")
qrimage.save(qrcodefilename)












We can specify both background color and filling color in the make_image() function as shown below.


import qrcode
qrcodedata = "https://www.dbblogger.com"
qrcodefilename = "c://temp//mywebsite.png"
qrObject = qrcode.QRCode()
qrObject.add_data(qrcodedata)
qrObject.make()
qrimage = qrObject.make_image(fill_color="red", back_color="yellow")
qrimage.save(qrcodefilename)










Decode a QR code using Python


The Python Opencv library can decode information from a QR code image. It consists of an inbuilt QR code detector function to interpret data from the QR code.

If the QR code is valid, it returns the data for which the QR code was generated.


import cv2
# read the QRCODE image
filename = "c://temp//mywebsite.png"
image = cv2.imread(filename)
detector = cv2.QRCodeDetector()
data, vertices_array, binary_qrcode = detector.detectAndDecode(image)
# if there is a QR code
# print the data
if vertices_array is not None:
  print("QRCode data:")
  print(data)
else:
  print("There was some error")


Read this article on how to easily create and edit QR Codes to suit your specific needs. It's relatively easy to do, and the only problem is that you might want to streamline the creation process by using a unique generator explicitly created for you!



1,950 views0 comments

Recent Posts

See All

Comments


bottom of page