Member-only story
How to Generate and Decode QR Codes in Python
A starter guide in handling QR codes in your Python app

By reading this piece, you will learn to generate your own QR code and decode QR codes from an image. At the end of this tutorial, you should be able to integrate QR codes functionality into your own Python application. I am using python-qrcode
module for this tutorial. Based on the official Github page, it outlines a QR code as:
“…a two-dimensional pictographic code used for its fast readability and comparatively large storage capacity. The code consists of black modules arranged in a square pattern on a white background. The information encoded can be made up of any kind of data (e.g., binary, alphanumeric, or Kanji symbols)”.
There is a big difference between QR codes and bar codes. Bar codes only hold information in the horizontal direction; a QR code holds information in both horizontal and vertical directions. As a result, a QR code contains a lot more information compared to a barcode.
There are four sections in this tutorial.
- Setup
- Generate QR code
- Decode QR code
- Conclusion
Let’s proceed to the next section and start installing the necessary modules
1. Setup
It is highly recommended to create a virtual environment before you continue with the installation. We will be using the following Python packages for this tutorial:
python-qrcode
— Python QR code image generator. Standard installation includespillow
as well for generating images.opencv-python
— Open-source library for computer vision, machine learning, and image processing. It comes with built-inQRCodeDetector
class, which helps to decode QR codes.
Installation is pretty straight forward via pip install
. Run the following command to install python-qrcode
and pillow
.
pip install qrcode[pil]
Once you are done, continue installing OpenCV-Python with the following command:
pip install opencv-python