The qrcode module: Generate your own QR codes using Python!

Featured Img QRCode Module

Hey there! Today we are going to learn how to get your own QR codes using the qrcode module in Python.

Seems interesting right? Let’s begin!

Introduction to QR (Quick Response) code

QR codes are able to store lots of data and when scanned, it allows the user to access the information instantly.

It stores all the data as a series of pixels in a square-shaped grid. In general, we use QR codes for the following purposes:

  1. Link app download link
  2. Accounts login details
  3. Making payments

The main components of a standard QR code are the three large squares outside the QR code. Once the QR Reader identifies them then it knows the whole information contained within the square.

Recommended read: How to create fake people details using the faker module?

Creating OR codes from scratch using the qrcode module

The first thing we do is importing qrcode module and then creating a qr object using the ORCode function.

The next step we have to code for is to add the data into the QR code using the add_data function. We pass the data we want in form of a string.

Next, we use the make function to build the QR code. The next step is to get the image of the QR code we build.

To create and save the QR code in form of an image we will make use of the make_image and save function respectively.

In the same function, we add the image path/name of the image. The code for the same is shown below.

import qrcode
qr = qrcode.QRCode()
qr.add_data('This is my first QR code.')
qr.make()
img = qr.make_image()
img.save('qr1.png')

The QR code generated is displayed below.

First Qr Code Generated Python
First Qr Code Generated Python

The image below shows the result when the QR code saved was scanned through my device.

First Qr Python Phone Output 1
First Qr Python Phone Output 1

Customizing the QR code

We can also customize the design and structure of the QR code by adding some properties in the qr object created earlier using the QRCode function.

Some properties that we are going to add in the object are as follows:

  1. version : This determines the size of the QR code and its value ranges from 1 to 40 ( 1 being the smallest obviously)
  2. box_size : This determines the no. of pixels need to be there in the QR box

We also added a few properties in the make_image function to change the color of the background and QR code by using the back_color and fill_color properties respectively.

The code for the generation of such QR codes is shown below:

qr1 = qrcode.QRCode(version=1,box_size=10)
qr1.add_data('My first customized QR code')
qr1.make()
img1 = qr1.make_image(fill_color="red", back_color="lightblue")
img1.save('qr2.png')

The output customized QR code is shown in the image below:

Second Qr Code Generated Python
Second Qr Code Generated Python

When scanned from my own device the result were accurate which is displayed below:

Customized Qr Python
Customized Qr Python

Conclusion

Congratulations! Now you can build QR codes for anything you want on your own. You can also add links instead of simple text for the QR code to reach a site when the QR code is scanned.

Hope you learned something! Happy coding!