Generate QR Code with 3 lines of Python code
The modern era has simplified a lot of hard labor tasks, one of which is encoding, mostly used in product identification systems. The Quick Response or QR Code is basically a barcode represented in a square format. These codes can easily be decoded from an everyday smartphone camera.
The recent pandemic situation has also taught us to go wireless in each possible format. QR codes can represent a variety of data to the receiver starting from Wifi Password, to Payment IDs.
Let’s move on to the tutorial ‘Generate QR Code with 3 lines of Python code’ and see the stepwise processes.
Installation
Installing Python
As a prerequisite, I hope you have python installed on your machine. In case you don’t, Install it from Python.org (opens in a new tab).
After installation is completed, Click Win and Search by typing CMD, then press Enter.
Type ‘python’ on the Terminal and press Enter. It’ll display the python version installed on your computer.
Installing Library
Apart from Python, we’ll also need a distinct python library named ‘Qrcode’.
Put the below command in your Terminal window. Wait for it to Download, and get installed on its own.
pip install qrcode
Upon installing you’ll be reported with a ‘Successfully installed’ message on the Terminal window.
Related: How to create an encryption Algorithm
Generating Code
In this portion, we’ll utilize the QRcode library to encode our information into a two-dimensional code and save that as an image on the local machine.
To write the python code you may use any Environment of online code runners. Personally, I find VSCode (opens in a new tab) the most eloquent platform to code in any language.
Create a new file named ‘QRCode.py‘ & start writing below provided code snippet.
import qrcode img = qrcode.make("https://soumyamondal.com")
img.save("myQR.jpg")
Let us be familiar with the flow of the code snippet, on how to Generate QR Code with 3 lines of Python code.
Start off by importing the library to our snippet.
Later the link is encoded into the form of an image. Nevertheless, if you’re willing to encode anything else than an URL, go on. Replace the content inside " " Inverted commas.
The very next line saves the image with your desired name in the very directory.
You might play along with syntax and customize it in any other way. However, this is one of the easiest as well as shortest paths to encoding & Generate QR Code with 3 lines of Python code.