Using Python you can generate your own QR code, customize it, and decode information from a QR code.
Setup a Python Environment
It is recommended to set up an isolated environment for Python projects. By doing this, you can understand more about the dependencies of the project.
Creating a Virtual Environment
You can create a virtual environment in Python in a number of ways: virtualenv, virtualenvwrapper, or Anaconda distribution. Here we will use virtualenv to create a virtual environment.
Create a folder using the mkdir [Folder] command, where you want to create this project. Type cd [Folder] in the command prompt to move into the new directory. Type virtualenv [Environment Name] to create a virtual environment. Activate the virtual environment by typing [Environment Name]\Scripts\activate
Install Required Packages
Install the required Python packages using the pip command. Make sure you have pip installed on your system.
Run the following command in your command prompt.
If you don’t want to install all packages in one go, you can install them one by one as:
This installs the opencv-python package which is mainly used for computer vision, machine learning, and image processing.
This installs the qrcode python package which is used for generating and reading QR codes.
This installs the numpy python package which is used for working with arrays.
This installs the Image python package which provides a number of functions to load images from files and to create new images.
Generate QR Code
To generate the code, create a new file with a .py extension which will have the code to generate the QR code.
Paste the following code in your Python file and run the program.
This will create a QR code image (MUOQRCode.png) for the given data (in this case, www.makeuseof.com). The generated QR code will look something like this:
Generate a Customised QR Code
You can customise the QR code with the amazing features of the qrcode library. You can change the fill color, background color, image size, box size and border thickness of the QR code.
Changing Image and Box Size
You can change the QR code image size using the version parameter in the QRCode class. It accepts an integer between 1 and 40 where 1 is equivalent to 21x21 matrix and 40 is equivalent to 185x185 matrix. Note that the data doesn’t fit in the specified size, the version will scale up automatically.
Similarly, you can change the box size using the box_size parameter in the QRCode class. It specifies the pixels of each box in the QR code.
The following QR code image file will be generated:
Also, the following output will be displayed-
Note that the version is automatically scaled up according to the size of the data.
Changing Fill Color
You can change the fill color of the QR code by using the fill_color parameter.
The following QR code image file will be generated:
Changing Background Color
You can change the background color of the QR code by using the back_color parameter.
The following QR code image file will be generated:
Changing Border Thickness
You can change the border thickness of the QR code by using the border parameter in the QRCode class.
The following QR code image file will be generated:
Decode QR Code Using QR Code Image
You can decode information from the QR code image using Python’s OpenCV library. OpenCV has an inbuilt QR code detector. Using the detector you can decode data out of the QR code.
If the image provided is a valid QR code, decoded data will be displayed. In this case, the following output will be generated-
Decode QR Code Live Using a Webcam
Most of the time people tend to use a webcam for scanning QR codes. Using the potential of Python and OpenCV library you can easily decode data from a QR code.
When you execute this code, your webcam will be automatically opened. Simply hold the QR code in front of the webcam and the data will be decoded and displayed in the command prompt.
Encoding and Decoding QR Code Made Easy
Using this article you can easily encode, decode and customise QR codes as you want. You can even create a complete QR code Scanner-Generator application using the code provided.
There are a number of creative ways to use QR codes. Get creative and use the QR codes as you want.