Generating QR-Codes in Python using QRCode Library

Hello everyone, In this tutorial, we will learn how we can Generate our own QR-Codes with just a few lines of codes. For this, we’ll be using ‘QRcode’ and ‘Pillow’ libraries. Pillow is a python imaging library(PIL) for image operations and manipulations which makes it easy to save our generated QR-Code as .png or .jpg format. Let’s start this tutorial with a brief introduction of QR-codes.

What is QR-Code?

QR-Code is a Quick Response Code which is is an optical image matrix that contains the data for which it is made. They are machine-readable codes whose data can be extracted with a QR-Coe Scanner and they are used in a wide range of applications like to store URLs omitting the need to write them, in-store product labeling, Tracking, Payments, etc.

Let’s make our own QR-Codes moving on with the installation of the required modules.

You can also generate QR code in Python using pyqrcode library

Installing QRcode in Python

To install the QRcode library run the following pip command.

pip install qrcode[pil]

This command will automatically install the QRcode library and all the dependencies including pillow in our python environment and you can refer to the official documentation here.

With the successful installation, let’s move forward making some QR-codes.

Making QR-Codes using Python

import qrcode

my_qrcode = qrcode.make(r'')
In the code above, Using qrcode.make(data), We have created a ‘qrcode.image.pil.PilImage object which contains our QRcode with the data we mentioned but because it is an object, therefore we will need a method that displays the Image of our QRcode, for that, we have used show() which will open the default image viewer with our QRcode.

The output of the above code –

Generating QR-Codes in Python using QRCode Library

Note that this will make a QRcode of default size. Now we will be changing the size and even the color of our QR-Code.

Modifying QR-Code in Python

In this section, we’ll be creating a modified QRcode having different background and code color, size and will save it.

import qrcode
mod_qrcode = qrcode.QRCode(
    version=2, box_size=5,
qrcode_image = mod_qrcode.make_image(fill_color="blue", back_color="yellow")'codespeedy_code.png')

We used QRCode class which allows us to modify our QRcode in which we have defined the following parameters.

  • version – The size of our QRcode is control by an integer value that ranges from (1 to 40). The default value is 1 corresponds to the matrix of dimension(21 x 21). The formula for dimension is (4*version + 17 dots each side). QRCode with a high version can store more data.
  • box_size – It serves as a purpose of zoom to our QRcode.
  • border – As per the name, It is the border width that we may want to apply to our QRcode.

QR code in Python – Color Background

After that, we have used make(fit=True) which means that the QRcode will automatically adjust the size to best fit in the window. Then, we have just made the image of our QRcode using make_image() with fill_color or pattern color as blue and back_color or background-color as yellow. They have default values of black and white respectively. In last we use save() to save the QRcode as an image.

This is how our QRcode will look.

QR code in Python - Color Background

We want you to generate your own QRCodes and Hope you like this tutorial. If you have any doubt feel free to comment below.

You may like to learn

Text watermark on an image in Python using PIL library

How To Convert Image To Matrix Using Python

One response to “Generating QR-Codes in Python using QRCode Library”

  1. Nirmit says:

    How to generate qr code which is in rgb format? Plz send source code.

Leave a Reply

Your email address will not be published. Required fields are marked *