Python Generate QR Code using pyqrcode module?

PythonServer Side ProgrammingProgramming

A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera. It is widely used for many commercial tracking applications and payment and website login etc. for various applications aimed at mobile-phone users. The pyqrcode module is used to generate the qrcocode in python. There are four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to store data efficiently.

Alphanumeric qrcode

We use the pyqrcode module. It has the cerate function which will be used to generate the qrcode. Finally we save it as a svg file and scale it to proper size as required. We can open the svg file and see it using the browser.

Example

import pyqrcode
from pyqrcode import QRCode

# String input for the QR code
str = "learnPython"

# Generate QR code
qrcd = pyqrcode.create(str)

# Create and save the svg file
qrcd.svg("qrcd.svg", scale=10)

Output

Running the above code gives us the following result −

Binary qrcode

We can also have a binary form of the qrcode using a similar code but with additional parameter. We just mark the mode as binary for the cerate function. The code looks slightly different to the human eye because of the level of compression.

Example

import pyqrcode
from pyqrcode import QRCode

# String input for the QR code
str = "learnPython"

# Generate QR code
qrcd = pyqrcode.create(str, mode='binary')

# Create and save the svg file
qrcd.svg("qrcd2.svg", scale=10)

Output

Running the above code gives us the following result −

raja
Published on 12-Jan-2021 13:46:08
Advertisements