Generate QR Code using Python

This article was first published on PyShark , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

This article will explore how to generate QR code in Python and some useful creation features from pyqrcode library.

Table of Contents

  • Introduction
  • Create a simple QR code image
  • QR code parameters
  • More QR code examples
  • Conclusion


QR codes recently became more popular than ever before, yet few people know that the first iterations of QR codes were created back in 1990s in Japan for the automotive industry.

QR (quick response) code is essentially a barcode that we are all used to see on the products we buy in grocery stores. It works the same way. QR code is a label that contains specific information.

Unlike a traditional barcode, QR codes are capable of storing more information and are often use to store product details, geolocations, coupons, URLs, and much more.

Due to its interesting capabilities of storing information, it became an area of interest in data science and machine learning mostly in analytics area.

If you think of a retail store that sells apparel, a simple QR code on each item can potentially store item description, colour, price, and other information. Once an item is purchased, that information can be retrieved from a POS system or data storage and further become a feed into a recommender system for example.

Now that we know what QR codes are and how they can be used. Let’s dive into actually creating our first simple QR code image and try to access the information using it.

Create a simple QR code using Python

To continue following this tutorial we will need two Python libraries: pyqrcode and pypng.

If you don’t have them installed, please open “Command Prompt” (on Windows) and install them using the following code:

pip install pyqrcode
pip install pypng

Import the required library:

import pyqrcode

Once the libraries are downloaded, installed, and imported, we can proceed with Python code implementation.

I will be creating a QR code which, when scanned, will take you to the this tutorial in your mobile device browser. To do this, I first need to find the URL to this post and store it as some variable:

dest = ''

The next step is actually creating the QR code object that will contain our link:

myQR = QRCode(dest)

Here we create an instance of .QRCode() class and pass our dest (destination) to it as an argument and in return it creates a QR code object.

We can take a look at it right away using:

And see:

QR code using Python

Very simple and easy to understand right?

To reuse this QR code, we will go ahead and save it as PNG:

myQR.png('qrcode1.png', scale=8)

Note: scale=8 is a parameter that adjusts the size of the QR code PNG, and you can adjust it to increase/decrease the size of the QR code image.

You can test this QR code using your phone’s camera (I used my iPhone) and it will take you to the URL of this article.

QR Code Object Parameters

The section above showed how to create a simple QR code without any specific adjustments or selected parameters. When they aren’t specified, they will take the default values and the code will be executed.

However, if we want to add some customization to it, it is worth discussing how and what we can adjust.

When we created our simple QR code, using the following line of code:

myQR = QRCode(dest)

In reality, there were certain preset default parameters, and, if expanded, the code would look like the following (yet produce the same output):

myQR = QRCode(dest, error='H', version=None, mode=None, encoding='iso-8859-1')

Let’s begin with listing all possible parameters of QRCode() class and discuss what each of them does:

  • content: this is the ‘target’ destination that we want to encode in the QR code.
  • error: error correction level of the code (by default set to ‘H’ which is the highest possible level).
  • version: specifies the size and data capacity of the code (can take integer values between 1 and 40) and when left unspecified, it will find the smallest possible QR code version to store the data that we want (knowing its size).
  • mode: specifies how the content will be encoded (there are four options: numeric, alphanumeric, binary, kanji). If left unspecified, it will be guessed by the algorithm.
  • encoding: specifies how the content will be encoded and defaults to iso-8.

Detailed explanation of each parameter is available here.

You can play around with our initial code and tune the above parameters to see how the differences will be represented in the final QR code image.

More QR code examples using Python

What is interesting is how adapted the smartphones algorithms are for QR code readings. In other words, when scanning these with an iPhone, Apple’s QR code decoders will know right away which app to use for each content of the QR code.

To test this, let’s try creating QR codes for: URL, address, and phone number:

dest = ['',
        '1 Yonge Street, Toronto, Ontario, Canada',
        '+1 (999) 999-9999']

for i in dest:
    myQR = pyqrcode.QRCode(i)
    myQR.png('myqrcode'+str(dest.index(i))+'.png', scale=8)

This will create and save three QR codes in the same directory where your script is located. For instance, each of these, when scanned, will be identified by an iPhone’s QR code decoder and apps to open them will be suggested automatically:

Link (will be prompted to open in Safari):

URL QR Code using Python

Address (will be prompted to open in Apple Maps):

Address QR Code using Python

Phone number (will be prompted to open in Phone and call):

Phone Number QR Code using Python

And interesting thing to notice is the sizes of each QR code. Essentially the way to think about it is the longer (larger) is the content you are trying to store in the QR code, the larger will be it’s size.

In addition, exploring the related documentation also shows the parameters of PNG output and explains how to adjust it (for example, make the QR code background green, and so on).

In conclusion, we only saved the QR codes we made in PNG formats, yet the pyqrcode library allows for great flexibility it terms of output formats of QR codes.


This article focused on exploring the process creating QR codes and saving it as PNG files using Python. It should be a good foundation to understand the process and have the knowledge to build on the functionality.

Feel free to leave comments below if you have any questions or have suggestions for some edits.

The post Generate QR Code using Python appeared first on PyShark.

To leave a comment for the author, please follow the link and comment on their blog: PyShark .

Want to share your content on python-bloggers? click here.