Generate a QR Code With Python

Anurag Singh Choudhary
6 min readJun 22, 2023

--

Introduction:

A QR code is a machine-readable barcode designed in two-dimensional pixelated form. A QR code can be used to store a range of data. The QR in the QR code spells out for Quick Response. The QR code was invented in 1994 by Masahiro Hara, a Japanese engineer from the Denso Wave car company, to track the movement of car parts.

The popularity of the QR code increased in the late 2010s with the improvement of the optical capabilities of mobile phones and their widespread adoption. Nowadays, QR codes are used for a wide range of applications such as making online payments, checking hotel menus, sharing Wi-Fi passwords, getting pricing and other product information, and much more. QR codes have become very popular that now each new smartphone comes with a built-in QR code reader application.

Image:1:Created By Author

Understanding Quick Response (QR) codes:

QR codes were initiated in the 1990s as a method to provide additional information than a standard barcode. They were developed by Denso Wave, a subordinate of Toyota, as a method to track cars throughout the manufacturing process.

Unlike barcodes, which require a beam of light to bounce off parallel lines, QR codes can be digitally scanned by devices such as mobile phones.

QR codes encompass black squares arranged in a grid (matrix) on a white history and are read by using specialized software this is capable of extracting information from the patterns gift in the matrix. These codes are capable of holding more information than traditional barcodes and primarily handle four data modes: alphanumeric, numeric, binary, and Kanji.

no matter the capacity of the accelerated record, QR codes have no longer been as popular with consumers as predicted. Rather than being created by consumers to share information, they are most often associated with advertisers and marketing campaigns.

QR codes have become widespread in facilitating digital payments and in cryptocurrency systems such as displaying a Bitcoin address. QR codes are also increasingly used to transmit net addresses to mobile telephones.

Quick response (QR) codes vs. bar codes:

Traditionally, the amount of information that can be communicated about a product or service has been limited by the amount of space on the product’s packaging or advertising promoting its benefits. If a consumer wanted more information about a product — availability, price, attributes — they would have to find a retailer or request additional documentation.

Barcodes are commonly found on the back of product packaging and convey data using a combination of different widths of parallel lines that can be read by machines with an optical scanner.

The barcode revolutionized the way companies managed inventory and pricing, and was first used practically in the 1960s by American railroads to track equipment and containers. Traditional, two-dimensional barcodes became common in US retail stores in 1974.

Barcodes are now found on everything from employee IDs and hospital wristbands to shipping containers.

Types of Quick Response (QR) Codes.

There are several types of QR codes that can be used for different items. They contain:

Micro QR Code: A smaller version of the traditional QR code used when space is limited. Micro QR codes can vary in size, but the smallest are 11 x 11 modules, encoding up to 21 alphanumeric characters.

Model 1 QR Codes: Model 1 is the framework of Model 2 and Micro QR. One to 14 versions are registered according to the AIMI (Automatic Identification Manufacturers International) standard.

QR codes Model 2: Model 2 has an alignment pattern for better positioning and contains more data density than Model 1. According to the AIMI standard, 1 to 40 versions are registered, with version 40 able to store up to 4,296 alphanumeric characters.

IQR code: Can be created in squares or rectangles in cases where space or shape is a problem. It can be in any of 61 formats.

SQRC: Contains a restricted read function that contains private information.

QR Frame: A customizable frame that can contain larger data in formats such as graphics, illustrations, or photographs.

Generating QR Code using Python:

Python is a programming language that provides various modules and packages that allow us to generate a QR code. In this tutorial, we will work with the qrcode package to generate the code.

However, in order to start working with the package, we need to install it.

Installing the Python QRcode package:

We can install the qrcode package using the pip installer using the following command:

syntax:

pip install qrcode  

The package will be installed on the system as a version of Python and pip.

Verifying the Installation:

In order to check whether the package has been installed in the system correctly or not, we can try to import the package and run the program.

Once the installation is complete, create a new Python file and enter the following syntax into it.

# importing the required module

import qrcode

Now keep the document file and run the file using the subsequent command in the command prompt.

$ python <file-name>.py  

If the program runs without throwing any import error, the module is installed correctly. otherwise, it is encouraged to reinstall the package and refer to its official documentation.

Now we will understand how to start working with the QRcode library.

Generating a Simple QR Code:

We can generate a simple QR code using the make qrcode function and pass the data as its parameter.

Let’s look at the following example that creates a QR code that reads “Welcome to medium”.

# importing the qrcode library
import qrcode
# creating a QR code with the make() function
qr_img = qrcode.make("https://medium.com/")
# saving the image file
qr_img.save("medium-img.jpg")

Output:

QR code of www.medium.com

Explanation:

in the code snatched above, we imported the qrcode library and create a variable that utilizes the make() function of the qrcode library to develop the QR code. We then store the code using the save() function to a directory.

Generating an Advanced QR Code:

Let’s look at the following example to generate a QR code that points to a Python tutorial.

Example:

# importing the qrcode module
import qrcode
# creating a QRCode object
obj_qr = qrcode.QRCode(
version = 1,
error_correction = qrcode.constants.ERROR_CORRECT_L,
box_size = 10,
border = 4,
)
# using the add_data() function
obj_qr.add_data("https://medium.com/")
# using the make() function
obj_qr.make(fit = True)
# using the make_image() function
qr_img = obj_qr.make_image(back_color = "black", fill_color = "cyan") #Background Color is Black
# saving the QR code image
qr_img.save("medium-img1.png")

output:

QR of www.medium.com

Explanation:

In the code snippet above, we have imported the qrcode library. We then instantiated the QRCode class of the qrcode library. We used different parameters to customize the QR code. We then used the add_data() function to include the information for the QR code. We also used the make() and make_image() functions to generate the QR code image. Finally, we saved the image file to a directory using the save() function.

Conclusion:

A quick response (QR) code is a type of barcode that can be easily read by a digital device and stores information as a series of pixels in a square-shaped grid. QR codes are often used to track product information in the supply chain and are often used in marketing and advertising campaigns.

QR codes are considered an advance over older one-dimensional barcodes and were approved as an international standard in 2000 by the International Organization for Standardization (ISO).

Key Takeaways:

  • Quick response (QR) codes are square arrays of dark or light pixels used to encode and quickly retrieve data using computing devices.
  • An improvement over traditional barcodes, QR codes can store much more information and are used in a variety of applications from supply chain management to cryptocurrency wallet addresses.
  • There are now several versions and variations of QR codes that are adapted to different purposes or that can store larger amounts of data.

--

--

Anurag Singh Choudhary
0 Followers

Passionate Machine learning professional and data-driven analyst with ability to apply ML techniques & various algorithms to solve real-world business problems.