How to Generate A QR Code Using Python

Unlocking the Power of QR Codes with Python

Natasha
5 min readJun 6, 2022

Introduction

QR codes can contain information (Text, Images, Audio etc.…) or links to information on websites, databases, apps, and online accounts. QR codes are increasingly becoming part of our daily life. We have seen QR codes used in architectural, retail, transportation, and fashion industries in previous years. However, the recent COVID-19 pandemic introduced QR codes for vaccination passports and saw its acceleration in grocery stores and bars. This article focuses on generating QR codes and bar codes. I will show you how to decode a QR code and create a QR code app in another article.

Photo by Albert Hu on Unsplash

Setup

Run the following code to install the required packages. You can find out more about the packages in the Github repo. There may be slight differences in how this is done depending on whether you are using Linux or Mac OS; please see the documentation for further assistance.

Barcode Package

The Barcode package is a Python library that provides functionality for generating and manipulating various types of barcodes. It offers a range of barcode formats such as EAN, UPC, Code 128, and more. The package allows you to create barcodes programmatically, customize their appearance, and render them as images or other formats.

!pip install python-barcode

QRCode Package

The QRCode package is a Python library specifically designed for generating and decoding QR codes. QR codes are two-dimensional barcodes that can store various types of data such as URLs, text, contact information, and more. The QRCode package provides an easy-to-use interface for creating QR codes with customizable properties like size, error correction level, and encoding format.

!pip install qrcode

Pyzbar Package

The Pyzbar package is a Python library that allows you to decode barcodes and QR codes from images or video streams. It provides functions to read and decode various barcode formats, including QR codes, using the ZBar library. The package supports input from image files or live video feeds, making it useful for applications such as scanning barcodes with a webcam or processing barcode images.

!pip install pyzbar

We can now run the following import statements. For barcode:

from barcode import EAN13from barcode.writer import ImageWriter

For qrcode:

import qrcodefrom PIL import Image

For pyzbar:

import pyzbar

QR Codes

The documentation states that:

QR Code is a Quick Response code is 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)

QR Code For A Business Card
The following QR code contains a link to my LinkedIn profile. By scanning the QR code, you’ll get access to a URL link where you can find information about my education, experience, courses and much more.

Code:

qrCodeLinkedIn. Created by the author

QR Code For An IoT Device (Digital Twin)
The following QR code contains information about a particular IoT device. By scanning the QR code, you’ll get access to a small dataset that you can load into a data frame, file or database.

Code:

qrCodeIOT. Created by the author

QR Code For Websites (article)
The following QR code contains information about a particular python article. By scanning the QR code, you’ll get access to 100 python projects for beginners.

Code:

qrCode100PythonProjects. Created by the author

QR Code For Certificates
The following QR code contains a portfolio of my certificates. By scanning the QR code, you’ll get access to a URL link where you can find evidence of my CPD certificates.

Code:

qrCodeCPD. Created by the author

Visit my Github repo for a closer look at the project.

Bar Codes

Barcode store alphanumeric data, as seen in the images below. QR codes can store alphanumeric data, symbols, pictures, audio, and other data types. Nevertheless, both have their benefits. The python-barcode package supports various formats. For this article, we will use EAN-13.

The documentation states that:

python-barcode is a pure-python library for generating barcodes in various formats. It’s 100% pure python.

Code:

EAN13 Bar code. Created by the author

Conclusion

Bar and Qr codes are a great way of storing information in a digital format. They are easy to use and display additional information in an easy to access format. This article demonstrated how to create barcodes and QR codes in Python using python-barcode, qrcode and pyzbar. After installing the packages locally using pip and importing the modules into the python environment, simple python code generated png image files containing links and data.

Thank you for reading!

Photo by Brett Jordan on Unsplash

Recommended Articles

100 Projects For Beginners Using Python | by Natasha Newbold | Apr, 2022 | Medium

Automate Searching Google Using Python | by Natasha Newbold | May, 2022 | Medium

Automate Text Messages Using Python | by Natasha Newbold | May, 2022 | Medium

Final Words

Generating QR codes using Python opens up a world of possibilities for data encoding, information sharing, and interactive experiences. With the right tools and libraries, the process becomes straightforward and customizable. Embrace the power of QR codes to enhance your projects, marketing campaigns, and overall digital presence. Let your creativity and Python skills shine through this versatile technology.

Thank you for reading this article! 🤗 I really appreciate it!

If you enjoyed this article, you can help me share this knowledge with others by👏clapping, 💬commenting, and be sure to 👤+ follow me.

Wait a second. To write on Medium and earn passive income, use this referral link to become a member. ✏️

Linkedin Profile. You can also view the code for previous articles on my Github.

--

--