QR Codes with Python

Thiago Waib
3 min readMar 21, 2022

--

In this article, we’ll be exploring how to use Python’s library Qrcode to generate QR Codes based on a set of data (string, URL, binary, etc…)

♦ The Basics

First of all, we have to install the Python interpreter (version >3.0). If you already have it installed you can skip this part, if not, you can do so via the Python official website.

After the installation, we can move on to installing the qrcode library. We can do so via the Python Package Installer “pip” on our command prompt:

pip install qrcode[pil]

or if you’re using macOS, then run

pip install qrcode"[pil]"

The [pil] will tell pip to also install the Pillow library, which will be used by qrcode to generate and save an image of the QR Code.

This should automatically install qrcode, which we can check by running

pip check qrcode

♦ Generating the QR Code

Now that we have everything set up, we can create our python script using any editor as long as we use the .py file extension (for this demo I’ll be creating mine as generator.py). On it we must do 2 things:

  • Import qrcode
  • Generate our QR Code data

☼ Converting our QR Code into an Image

Now that we have our data finally converted into a QR Code inside our qr object, we can start working on converting this code into an image that can be shared and read with compatible devices (like you’re smartphone i.e).

  • If you are looking to save your newly generated QR Code to a custom path, you can add these steps

Now if everything went smoothly, you have generated your own custom QR Code! :D

You can also experiment with other properties of the qrcode library, such as styling your image or saving it as .svg; read more about this at the qrcode repository.

As always, thank you for reading through this tutorial! If you want to take a further look on my personal take of this algorithm, you can read this QR Code

https://github.com/thiagowaib/code-compendium/tree/main/qrcode-gen
Link to my QR Code Generator

--

--

Thiago Waib

Hello ツ I’m Thiago, a web developer and software enthusiast. You can follow my work here on Medium or at my Github → github.com/thiagowaib