Exploring the QR Code Library in Python

Greg Park
Strategio
Published in
3 min readAug 5, 2022
Photo Credit: Medium.com

Scenario

Today we are going to learn how to create a QRCode using the Python QRCode Library.

Prerequisites

  • Must have Python 3+ installed on your local system as well as a code editor or IDE.

Environment Set Up and Installations

If you install Python packages on your local system globally, the newly installed files could break system tools or other projects. To prevent this, we will create a virtual environment using the Python venv (short for Virtual Environment) module.

Open a terminal window and create a directory, qrcode, to hold this project in the location of your choosing.

> mkdir qrcode

Navigate inside the qrcode directory.

> cd qrcode

Now we want to instantiate the virtual environment using the venv module.

> python3 -m venv venv

This will create a folder in your current directory, named venv, containing the virtual environment. In order to activate the virtual environment, we enter:

> source venv/bin/activate

source is a built-in bash shell command that executes the content of the file passed as an argument. You should now see (venv) at the beginning of your current line in the terminal.

Now that we have a working virtual environment, we will proceed with installing the QRcode library.

In order to be able to save our QRcode as an image we need to also install the Python Pillow Library. The Pillow Library is a fork of an older library called PIL which stands for Python Imaging Library.

> pip3 install qrcode
> pip3 install pillow

Once the installations are completed, we will create a file “qrcode_reader.py” in our current directory and open it with the code editor of our choice. I will be using Visual Studio Code throughout this tutorial.

> touch qrcode_reader.py
> code qrcode_reader.py

The touch command is used to create a file with the argument being the name of the file being created. The code command opens up the file chosen in Visual Studio Code. If you do not have Visual Studio Code on your system then ship this line.

The Code

Let’s break the code down line by line.

The import keyword in Python is used to make code in one module available in another module. This will make the functions and classes in the qrcode module available for use in our current file.

import qrcode

A variable then needs to then be created to hold a string containing the data to be encoded. This can be any plain text or a URL.

data = 'QRCode Data Example'

For this tutorial, we will be using the simple method of creating a QRCode by using the make and save functions as a shortcut. A more advanced method that allows more flexibility and control over the QRCode creation can be explored here.

img = qrcode.make(data)img.save('MyQRCode.png')

That is all it takes! Now if you run the program, a PNG image of the QRCode containing our data should be created in the same directory as the file.

Next time we will explore how to use Python to read and decode QRCodes!

Was this walkthrough helpful? Confusing? If you have any questions feel free to comment below! Make sure to follow if you’re interested in similar content and want to keep learning alongside me!

--

--