How to create a QR code using Python

In this tutorial, we will learn how to create a Quick Response or QR code using Python, the QRCode package and hashlib.

How to create a QR code using Python
Picture by Pixabay, Pexels.
6 minutes to read

The QR code, or Quick Response Code, works by encoding data in a two-dimensional barcode. It is a type of matrix barcode, which is a two-dimensional barcode that uses a matrix of dots to represent data.

The QR code was first designed in 1994 by Denso Wave, a Japanese subsidiary of Toyota, and was first used in the automotive industry. It was later adopted by the Japanese retail industry, and in 1999, the QR code was made available to the public.

The QR code is now used in a wide variety of applications, including mobile phone applications, websites, and even in the medical industry. Cleverly, it also includes an error checking element that allows the code to be read even if it is damaged.

In this tutorial, we will learn how to create a QR code using Python, the QRCode package and hashlib. To read a QR code using Python, check out my other tutorial which explains how to do this using OpenCV.

How do QR codes work?

Regular barcodes, also known as linear barcodes, are one-dimensional codes that are made up of a series of lines of varying widths. They can only store a limited amount of information, and they can only be read by specialized barcode scanners. In contrast, QR codes are two-dimensional codes that are made up of a grid of black and white squares.

They can store much more information than regular barcodes, and they can be read by almost any smartphone or other device with a camera. Additionally, QR codes can be read from any direction, whereas regular barcodes must be scanned from left to right.

The black and white squares in a QR code represent binary data. Each square is either black or white, and the pattern of squares encodes the information that the QR code is meant to convey. The position and arrangement of the squares are determined by a complex algorithm that ensures that the code can be read accurately even if the QR code is partially obscured or distorted.

The squares in the corners of a QR code are called alignment patterns. They are used to help a QR code reader determine the orientation of the code and to correct any distortion that may have occurred. The alignment patterns are not necessary for the QR code to be read, but they can improve the reliability of the reading process.

How do QR code scanners work?

QR code scanners, also known as QR code readers, use a camera on a smartphone or other device to take a picture of a QR code. The scanner then processes the image to extract the data encoded in the QR code.

This typically involves using software to analyze the pattern of black and white squares in the code and to convert that pattern into the information that the QR code is meant to convey. The scanner may then take some action based on the information, such as opening a web browser and going to a URL.

The algorithm for generating QR codes is public and has been published by the ISO, the international organization that sets standards for QR codes, so Python developers have created code that can both create and read QR codes.

The algorithm is complex and involves several steps, including generating the data payload, adding error correction data, and determining the placement of the black and white squares in the QR code. Thankfully, you don’t need to understand the QR code algorithm to create or scan QR codes.

Install the packages

In this project we’ll be writing a simple Python function that takes some text and encodes it in a QR code. It will take the text, use the hashlib library to create a unique filename, and then use the QRCode package to create the QR code. To get started, open a Jupyter notebook and install the qrcode package using Pip. There’s no need to install hashlib, as it’s already part of Python so is pre-installed.

!pip3 install qrcode

Import the packages

Once you’ve got qrcode installed, import the hashlib and qrcode packages.

import hashlib
import qrcode

Create a Python function to create a QR code

Next, we’ll create a Python function to take some data from the user and encode it in a QR code using the qrcode package. After we’ve created the QR code image, we’ll take the data and use hashlib to md5 encode it and create a filename unique to the text provided. We’ll then save the image and return the filename of the image created.

def create_qr_code(data):
    """
    Create a QR code from a string.
    """
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data(data)
    qr.make(fit=True)

    img = qr.make_image(fill_color="black", back_color="white")
    filename = hashlib.md5(data.encode()).hexdigest() + ".png"
    img.save(filename)
    
    return filename

To run the function, you simply need to pass some text (which can include a URL if you wish) to the create_qr_code() function. It will generate the QR code image and return its filename, so you can save it in use it elsewhere in your project.

filename = create_qr_code("Follow me on Twitter at https://twitter.com/EcommerceMatt")
filename
'2b6e2172691736f49be5f257ba1c6327.png'

QR Code

Matt Clarke, Friday, September 30, 2022

Matt Clarke Matt is an Ecommerce and Marketing Director who uses data science to help in his work. Matt has a Master's degree in Internet Retailing (plus two other Master's degrees in different fields) and specialises in the technical side of ecommerce and marketing.