In this article, we will learn a python QR code generator using the pyqrcode library. Python has different libraries to generate QR codes, like – qrcode, PyQRCode, PyQRCodeNG, qrcodegen, Sengo. Each library has its own advantages.
pyqrcode module is a QR code generator. The module automates most of the building process for creating QR codes. This 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.
What is a QR code?
A QR (quick response) code is a box-shaped matrix form of a 2-dimensional barcode (also called checkerboard-type barcode) that contains some meaningful data or linkage. It is frequently employed on mobile apps and websites nowadays. As compared to the traditional barcodes, these data can store more information. Such code renders a certain box-type pattern across it for recording meaningful data or a large set of information in a black and white pattern.
QR code was invented by a Japanese engineer Masahiro Hara from automobile manufacturer Denso Wave in the year 1994 to track the movement of car parts.
QR Code has increased in popularity in the later 2010s with improvement in optical capabilities of mobile phones and their wide adoption.
Such a code remains distributed on a 2D plane. It can hold numbers, English alphabets, Japanese letters, Chinese characters, special symbols (Unicode and ASCII), binary information, and other information within that square image. The corresponding appearance of dots (square dots or other shapes) represents binary “1”, and the absence of these dots or white/blank spaces represents binary “0”. A QR code leverages a permutation and a combination technique to determine the meaning of the matrix two-dimensional bar code.
QR code Library in Python (pyqrcode)
pip install pyqrcode
Once you have successfully installed the pyqrcode library. At First, we have imported the pyqrcode module and used the create() method to generate the code.
Syntax of QRCode create() method –
pyqrcode.create(content, error='H', version=None, mode=None, encoding=None)
When creating a QR code only the content to be encoded is required, all the other properties of the code will be guessed based on the contents given. This function will return a QRCode
object.
One can specify all the properties of the required QR code through the optional parameters of the pyqrcode.create()
function. Below are some properties:
error: This parameter sets the error correction level of the code.
version: This parameter specifies the size and data capacity of the code.
mode: This parameter sets how the contents will be encoded.
In the given example, we have passed the string as an argument to this method and used the svg() method to store it in svg file format.
# Import QRCode from pyqrcode
import pyqrcode
from pyqrcode import QRCode
# URL string
site = "www.rocoderes.com"
# Generate QR code
getqrcode = pyqrcode.create(site)
# save in svg file format
getqrcode.svg("qrcode.svg", scale = 8)
Output of QR code Generator:
When you execute the above file, it will generate and save the file in your project directory.
Create QRCode image in png format
Python provides a pypng library to read and write on png file images. If we want to save the generated QRcode in a png file, we will have to install this library.
Run the following command to install ‘pypng’ library
pip install pypng
Here is the code to generate qrcode and save it in a png file.
import pyqrcode
import png
from pyqrcode import QRCode
# URL string
site = "www.rocoderes.com"
# Generate QR code
getqrcode = pyqrcode.create(site)
# save in svg file format
getqrcode.svg("qrcode.svg", scale = 8)
# save in the png file
getqrcode.png("qrcode.png", scale = 6)
Full Source Code Of Python QR Code Generator
import pyqrcode
import png
from pyqrcode import QRCode
site = "www.rocoderes.com"
getqrcode = pyqrcode.create(site)
getqrcode.svg("qrcode.svg", scale = 8)
getqrcode.png("qrcode.png", scale = 6)