How to Generate QR Codes Using Python
What is a QR Code?
A QR code (Quick Response code) is a two dimensional barcode used popularly in product tracking, item recognition, document tracking and general marketing. It is also now commonly used in entry tickets since it can be easily scanned using a QR code scanner in a mobile. Standard QR codes can contain upto 3Kb of data. See this page to know how a QR code stores data.
QR codes may be used to display text, encode a web page URL etc. In the following python example, we will encode the URL of this website. Hence by scanning the resulting QR code image, you can navigate to www.quickprogrammingtips.com.
Generating QR Code Using Python
The following example uses pyqrcode module available from python repository. It is an easy to use module written in pure python. Optionally you can use it along with pypng module to render png files.
Before writing and running the following sample python program, ensure you have pyqrcode installed by running the pip command,
If you plan to create png files as qr code output, install pypng as well,
Let us now quickly create a simple QR code which contains the data, "www.quickprogrammingtips". By scanning the QR code, you can visit this site. Type the following code in a file named qrcodegenerator.py,
# Import pyqrcode module. Ensure it is installed using pip3 install pyqrcode import pyqrcode # Import png module. Ensure it is installed using pip3 install pypng. import png from pyqrcode import QRCode data = "www.quickprogrammingtips.com" # create qr code using the alphanumeric encoding of the data (url string in this case) qrobject = pyqrcode.create(data) # Create and save svg and png files qrobject.svg("siteqrcode.svg", scale = 5) qrobject.png('siteqrcode5.png', scale = 5) qrobject.png('siteqrcode1.png', scale = 1) qrobject.png('siteqrcode10.png', scale = 10)
Run the above code using,
The above code generate qr codes in svg and png format in the local folder. We have also created 3 png files with different scale parameters. A scale value of 1 defines the data module size of 1 pixel and hence will be too small. A scale factor of 5 or more creates larger image sizes that can be easily scanned. With a scale factor of 5, qr code images created was around 200px in width and height.
The above code used a simple version of the pyqrcode.create() function. For advanced use it can take additional parameters such as error level, qr version and mode. A typical use case will be - pyqrcode.create('hello', error='L', version=27, mode='binary'). Let me explain what these additional parameters are,
- Error level - QR codes can use one of four possible error correction values. They are referred to by the letters: L, M, Q, and H. The L error correction level corresponds to 7% of the code can be corrected. The M error correction level corresponds to 15% of the code can be corrected. The Q error correction level corresponds to 25% of the code can be corrected. The H error correction level corresponds to 30% of the code can be corrected.
- Version - QR codes have versions 1 to 40. Higher version can hold more data with increased number of dots. When not specified, lowest viable version will be selected based on the data encoding and data correction level.
- Mode - The encoding used to represent the data in a QR code. There are four possible encodings: binary, numeric, alphanumeric, kanji. When not specified, the library will pick one based on the data passed.
The following example creates a QR code for the text "Hello World" using the advanced parameters.
# Import pyqrcode module. Ensure it is installed using pip3 install pyqrcode import pyqrcode # Import png module. Ensure it is installed using pip3 install pypng. import png from pyqrcode import QRCode data = 'Hello World' # create qr code using the alphanumeric encoding of the data # Since the data contains a space qrobject = pyqrcode.create(data, error='H', version=10, mode='binary') # Create and save svg and png files qrobject.png('messageeqrcode5.png', scale = 5)
If you specify a lower version number that cannot hold the data you want to encode, you will get the follwing error,
The data will not fit inside a version code with the given encoding and error level.