create and read QR code Python

QR codes are an important part of today’s world. They are used in many modern applications to store large data visually easily. They are nowadays used in many money transaction apps for easily getting the information by scanning the QR Code. They are also used by applications to give authentication to another device by scanning the Code easily.

QR codes or Quick Response Codes is a two-dimensional bar code consisting of some black squares into a white background. Due to QR codes’ ability to store more data and fast readability, it gained huge popularity over the bar codes, which are composed of black bars and white spaces in a single dimension.

In this tutorial, we see how to generate and read QR codes programmatically using Python. To follow this tutorial, you must have python installed in your system. It is also recommended to use an IDE like the open-source Visual Studio Code for writing the Code.

The pyqrcode module

In python, we have a module named pyqrcode, which eases creating QR codes in python. It follows all the standards for creating QR codes from the text. It has a class with the name QRCode that can be used to create a QRCode object, followed by using this object to add data and generate the QR Code. See the following installation procedure to install pyqrcode and all the other required libraries.

Installation

Let us begin our installation process for all the required libraries. It is first recommended to create a virtual environment to install and use the python libraries, which will separate the libraries’ installation process. If you don’t know how to create and use virtual environments in Python, then follow our guide on creating virtual environments in python. After creating the virtual environment, we can install the packages. We will use the pip package manager to install the libraries. This tutorial will use the PyPNG, OpenCV, and pyqrcode libraries. We can install all these libraries by running the following command in the terminal.

pip install pypng opencv-python pyqrcode

Generating a QR Code

The pyqrcode library has a QRCode class that can be used to create a QR code. This class accepts a string containing the text we want to store in the QR Code. See the below code for an illustration.

# Importing the required modules
import pyqrcode  
# creating a string data to store in the QR Code 
data = 'name : Code Underscored \nLocation: Internet'
# creating the QR Code object
qr = pyqrcode.QRCode(data)
# displaying the QR Code
qr.show()

We used the QRCode class of the pyqrcode module to create a QR code object and given it string data as an argument. Then we use the show() method of the QR Code object to display the QR code.

Output:

Generating a QR code
Generating a QR code

We can also display the QR Code into the terminal. The QR Code object provides a method called terminal that returns text data, which can be used to print the QR Code in the Terminal. See the below code for an illustration.

# Importing the required modules
import pyqrcode  
# data to be inserted in the QR Code
data = 'https://thirdeyemedia.wpmudev.host/codeunderscored/'
# creating the QR Code object with the data
qr = pyqrcode.QRCode(data)
# using the terminal method to get the text for QR code
text = qr.terminal()
# displaying the QR Code
print(text)

The QR code generated in the terminal is completely valid we can check it by scanning using a Code scanner. See the below image for the output I have on running the code on my machine.

outputting the QR code in the terminal
Outputting the QR code in the terminal

The QRCode class also gives some other options to configure. The following list shows all the parameters available.

  • error: This parameter is used to set the error correction level of the QR code.
  • version: This parameter is used to specify the size and data capacity of the QR code.
  • mode: This parameter configures how the contents should be encoded.
  • encoding: This parameter is used to configure which encoding should be used.

We have seen how to create a QR Code and display it, but displaying a QR Code not enough; we also want to save the QR Code for later use.

Saving a QR Code in SVG format

SVG images are one of the most popular vector image formats for the web. We can use it on websites for its good performance. The pyqrcode library gives us an option to save the QR Code in an SVG format. We need to use the svg() method of the QRCode object. See the below code for an example.

# Importing the required modules
import pyqrcode  
# data to be inserted in the QR Code
data = 'https://thirdeyemedia.wpmudev.host/codeunderscored/'
# creating the QR Code object with the data
qr = pyqrcode.QRCode(data)
# saving the QRCode in the SVG format
qr.svg('qrcode.svg', scale=8)

In the above code, we use the svg() method of the QR Code object with two parameters. The first parameter is the name of the file where we want to save the SVG data. The second parameter is the scale which represents the size of the QR Code. On running the above code we will have a new SVG file created with the following data.

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" height="328" width="328" class="pyqrcode"><path transform="scale(8)" stroke="#000" class="pyqrline" d="M4 4.5h7m2 0h1m1 0h1m2 0h1m3 0h1m2 0h2m1 0h1m1 0h7m-33 1h1m5 0h1m2 0h2m1 0h1m2 0h3m2 0h1m1 0h1m1 0h1m1 0h1m5 0h1m-33 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m3 0h2m1 0h4m1 0h1m1 0h3m1 0h1m-33 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h2m1 0h1m1 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-33 1h1m1 0h3m1 0h1m2 0h3m1 0h1m3 0h1m1 0h1m3 0h2m1 0h1m1 0h3m1 0h1m-33 1h1m5 0h1m2 0h1m5 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m1 0h1m1 0h2m1 0h1m1 0h1m3 0h5m3 0h1m-25 1h2m2 0h3m5 0h1m3 0h1m3 0h3m-31 1h7m1 0h3m1 0h1m1 0h1m1 0h1m1 0h7m1 0h1m1 0h1m1 0h1m-31 1h1m5 0h1m3 0h1m1 0h3m1 0h1m1 0h1m4 0h2m3 0h4m-32 1h1m1 0h3m1 0h1m1 0h1m2 0h3m1 0h1m1 0h3m1 0h1m1 0h7m1 0h1m-32 1h1m1 0h3m1 0h1m1 0h2m1 0h2m1 0h3m8 0h1m1 0h1m4 0h1m-33 1h1m1 0h3m1 0h1m2 0h1m1 0h1m4 0h2m3 0h1m1 0h2m3 0h2m1 0h2m-33 1h1m5 0h1m2 0h1m1 0h1m1 0h2m1 0h2m4 0h1m2 0h2m2 0h4m-33 1h7m2 0h1m1 0h1m3 0h1m1 0h1m3 0h4m1 0h1m1 0h1"/></svg>

You can check the SVG image by clicking the run button present in the top of the above code block.

Saving a QR Code in PNG format

We can also save a QR Code into the PNG image format. To save a QR Code in a PNG format we need to use the png() method of the QRCode Object. See the below code for an illustration.

# Importing the required modules
import pyqrcode  
# data to be inserted in the QR Code
data = 'https://thirdeyemedia.wpmudev.host/codeunderscored/'
# creating the QR Code object with the data
qr = pyqrcode.QRCode(data)
# saving the QRCode in the PNG format
qr.png('qrcode.png', scale=10)

Output:

saving the QR code in PNG format
Saving the QR code in PNG format

Reading a QR Code

The pyqrcode can’t be used to read a QR code in python so we need to use another library called OpenCV. OpenCV is a python library that is mainly used for performing Computer Vision tasks. This library is written in C++ and can be used in python. The OpenCV provides a class called QRCodeDetector that can be used to read a QR Code. See the below code for an illustration.

# importng the required modules
import cv2
# reading the image qrcode.png that we created before
image = cv2.imread('qrcode.png')
# creating a QRCodeDetector object
qr_det = cv2.QRCodeDetector()
# using the detectAndDecode object
data = qr_det.detectAndDecode(image)
# printing the data
print(data[0])

In the above code, we use the QRCodeDetector class of the cv2 module to create a QRCodeDetector object. This class has a method called detectAndDecode() that returns a tuple of data.

Output:

reading a QRcode
reading a QRcode

Conclusion

In this tutorial, we have learned how to create and read QR Codes programmatically using Python. This method helps build applications that can share data using QR Codes. You may also refer to our article on working with PDFs in python.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *