QR code in python is easy to read and generate. We’ll show you how to create and read a simple and complex QR code in this lesson.
Table of Contents
What exactly is a QR code?
QR codes are pixelated two-dimensional barcodes that can be scanned by machines and used to store a variety of information. In a QR code, the letters QR stand for Quick Response.
The QR code was invented in 1994 by Masahiro Hara of Denso Wave, a Japanese automobile manufacturer, to track the movement of car parts.
QR codes became more popular in the late 2010s as mobile phone optical capabilities improved and their use became more prevalent.
QR codes are now used for a variety of things, such as making online payments, reviewing hotel menus, sharing wifi passwords, getting product prices and other information, and so on.
These codes have become so popular that every new smartphone now comes with a built-in QR code reader.
Generate a QR Code in python using a module
Set up the QR Code module.
The QRcode package will be used to generate QR codes.
The first step is to use the pip command to install the package.
## QR Code in python example pip install qrcode[pil] ##
Create a simple QR code
A simple QR code can be created by using the make function of QRcode and passing the data as an argument.
The following code creates a QR code that says “Hello World.”
## QR Code in python example import qrcode img = qrcode.make('Some data here') # qrcode.image.pil.PilImage type(img) img.save("some_file.png") ##
Create an advanced QR Code
You can create an advanced version of the QR code for more control. Use the following method to generate a QR code.
## https://github.com/lincolnloop/python-qrcode ## QR Code in python examples import qrcode qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data('Some data') qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") img.save("img_name.png") ##
The size of the QR Code is controlled by the version parameter, which is an integer between 1 and 40. Set to None and use the fit parameter to automatically decide this while writing the code.
When utilizing the default image factory, fill color and back color can be used to modify the backdrop and paint color of the QR. RGB color tuples are accepted by both parameters.
How to read a QR code in python?
Scanning and reading a QR code is a straightforward process. The path to the QR code must be specified as an argument when creating the QR object.
Assume we’re attempting to decode the QR code generated at the start of this post.
## from qrtools import QR qr_code = QR(filename = "qr_code.png") # decodes the QR code and returns True if successful qr_code.decode() print (qr_code.data) ##
We can also print the values of the other parameters supplied while generating the QR object to generate the QR code.
For example, if we use the same QR code as before, adding these print statements will result in the following additional output.
## print (qr_code.data_type) print (qr_code.pixel_size) print (qr_code.margin_size) ##