Generate QR code using python – pyqrcode

In this article we are going to see how we can generate qr code in python using the pyqrcode module. There are also various libraries available to perform this process. This one is simple and easy to use.

Let us get to the process.

pyqrcode

The pyqrcode module is a QR code generator that is simple to use and written in pure python. The module is compatible with Python 2.6, 2.7, and 3.x. The module automates most of the building process for you. Generally, QR codes can be created using only two lines of code!

Unlike other generators, all of the helpers can be controlled manually. You are free to set any or all of the properties of your QR code.

QR codes can be saved as SVG, PNG (by using the pypng module), and plain text. They can also be displayed directly in most Linux terminal emulators. PIL is not used to render the image files.

The pyqrcode module attempts to follow the QR code standard as closely as possible. The terminology and the encodings used in pyqrcode come directly from the standard. This module also follows the algorithm laid out in the standard.

Installation

Before going to write a code you first need to install this module in your terminal or command prompt. For that first you need to open your terminal and write command as

$ pip install pyqrcode

Or from the terminal:

$ python setup.py install

also install the pypng module

$ pip install pypng

Code

  • Show qr code
  • Save qr code
  1. To save the qr code as png image we can use the following code.

2. To save the qr code as svg image we can use the following code.

This will generate an svg file like this,

  • Convert json to qr code

Any json or dict can be converted into qr code by simply converting the dict to a string and then passing it to the qrcode.create() function

Saving options

There are three options to be considered while saving the file. They are

  • scale (specifying different scale will generate qr codes with different aspect ratio)
scale=2
scale=6
scale=10
  • background (requires rgba color code to set the bg of the qr code)
background=(0x2f, 0xff, 0xff, 0x88)
  • module_color (requires rgba color to set the module color). It is the black part of the qr code.
module_color=(0x66, 0x33, 0x0)

There you go. Now you can also generate qr codes instantly with the help of python and pyqrcode and integrate in your applications.

Applications of QR codes in web development

  1. Use QR codes to create landing pages and capture email addresses. 
  2. Since QR codes can be URLs, you can craft custom tweets that post to the person’s Twitter account when they scan the code, making it easier for them to share your specials and offers.
  3. Billings and other stuffs.

What product are you going to use this for?

Happy coding!