In today’s society, we want to access information safely and conveniently at all times. You will learn How To Generate QR Codes With Python and decode QR codes from an image by reading this article. Nobody likes reading and clicking on long URL links or word sequences. You should be able to include QR code features into your Python application by the end of this course.
In addition, in the aftermath of the recent pandemic, it is generally thought to be best to avoid physical contact and conduct transactions without it. For this tutorial, I’m using the python-qrcode package.
This goal is accomplished through the use of bar codes and QR codes. The development of QR codes solves some of the space issues that plague bar codes. A QR code is defined as “…a two-dimensional pictographic code used for its speedy readability and fairly substantial storage capacity.” On a white background, the code is made up of black modules arranged in a square pattern. Any type of data (binary, alphanumeric, or Kanji symbols) can be used to encode information.”
QR codes are an excellent tool for recording product information, transferring data, leading visitors to a landing page or website, downloading apps, paying bills (at restaurants or elsewhere), purchasing, e-commerce, and much more.
Between QR codes and bar codes, there is a significant difference. A QR code carries information in both horizontal and vertical orientations, whereas a bar code only holds information in one direction. As a result, as compared to a barcode, a QR code provides a lot more information.
How To Generate QR Codes With Python – This tutorial is divided into a few pieces.
- All Necessary Libraries are Installed
- Create a QR code
- Decrypt a QR code
How To Generate QR Codes With Python
Total Time: 10 minutes
Installing All Necessary Libraries
Before continuing with the installation, it is highly advised that you construct a virtual environment. This tutorial will make use of the Python packages listed below.
To get started with this project, we’ll need to install a few Python libraries that will allow us to build, generate, and decode QR codes successfully on our machine. The QR code library will be the first library we install for encoding our QR codes.
python-qrcode— Image generator for QR codes in Python. Pillow is also included in the standard installation for creating images.
opencv-python is an open-source computer vision, machine learning, and image processing toolkit written in Python. It has a built-in QRCodeDetector class that aids in the decoding of QR codes.
The command to get started with the installation of the qrcode library module is simple: pip install qrcode. If you have the path of the environment variables configured appropriately, you can add the following line of command either in the virtual environment of your choice or directly in the command prompt (or windows shell). The pip install command makes installation a breeze. To install python-qrcode and pillow, run the following command.
pip install qrcode
Make sure you have the Pillow library installed in the Python environment you’re using. This library is one of the best for working with photos and performing most visual aesthetics-related tasks. The alternative command in the command block below will install both the required libraries as well as the pillow library for creating the images of the QR codes that will be stored.
pip install qrcode[pil]
After that, run the following command to finish installing OpenCV-Python:
pip install opencv-python
Make sure the opencv-python version is at least 4.3.0 if you want to identify several QR codes in a single image. The multi detection features are not available in older versions.
Let’s move on to the next step and get some Python code written.
Create a QR code
We will use the qrcode package to encrypt our data and create a quick response code that we will be able to access with some decoding. Please follow the code snippet supplied below to generate the QR code, and you will get an image of the specific QR code with the appropriate data saved in it.
# Create The variable to store the information
# link = "https://.com/"
data = "Hello! Welcome To Technozive!"
# Encode The Link
img = qrcode.make(data)
# Save the QR Code
Insert the following import statement at the start of your Python code.
from PIL import Image
Simply use the create() method to build a QR Code based on your provided text for basic use:
img = qrcode.make('Your input text')
You may use the QRCode class, which has many more controls and attributes.
qr = qrcode.QRCode(
version — Accepts an integer between 1 and 40 to specify the size of the QR Code. The smallest version 1 is 21 by 21 in size.
box size — Specifies the number of pixels for each QR code box.
border — Sets the thickness of the boxes’ borders. The default value is 4, which represents the smallest size.
error correction — This variable governs the error correction utilized. It will be detailed more in the next paragraph
Save as an image file
The next step is to use the add data() method. Enter the input text of your choosing. Continue by attaching the following code to make a QR code with a white background and black fill.
img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
You may save it as an image file by doing the following:
You should see a newly created QR code, similar to the one seen:
Overlay a picture on top of a QR Code.
You may use a pillow to overlay a picture on top of your QR code. To open a picture, let’s use the image function Object() [native code] . The thumbnail() function aids with picture resizing. You should resize it to a lower resolution to avoid obstructing the full QR code.
You may have noticed that some of the QR codes have an overlay graphic on top of them. This helps to advertise your business while also avoiding misunderstanding with other QR codes. If you wish to do so, make sure to choose the optimal error correction constant to avoid problems with incorrect decoding.
logo_display = Image.open('profile.png')
Using the paste() method, call the following code to compute the center location and overlay the thumbnail on top of the QR code.
logo_pos = ((img.size - logo_display.size) // 2, (img.size - logo_display.size) // 2)img.paste(logo_display, logo_pos)
Save it using the save() method, just like we did before.
Here’s an illustration of what it’ll look like:
Decrypt a QR code
In this step, I will decipher the QR codes that I created before. For this, I propose that you create a new Python file.
The picture above is a depiction of a QR code that contains a link. If you’re trying to view a QR code containing a URL link, consider using a smartphone app for decoding such QR codes or any other app to read the QR code.
QR code scanners may be used to decode URL links like the one seen above.
However, if you are attempting to access encrypted data, you can decode the information using Open-CV. The QR Code Detector feature allows the user to decode a specific QR code picture and retrieve the data included in the QR code. Check out the code sample below for a quick overview of how to do the following action.
# Create The Decoder
decoder = cv2.QRCodeDetector()
# Load Your Data
file_name = "test1.jpg"
image = cv2.imread(file_name)
# Decode and Print the required information
link, data_points, straight_qrcode = decoder.detectAndDecode(image)
Insert the import statement below.
cv2 should be imported as cv
We may use the QRCodeDetector to perform the following tasks:
detect— Finds a QR code in an image and returns the quadrant that contains the code.
decode— Decodes a QR code discovered in an image via the detect() function. If the code cannot be decoded, it returns an empty string or a UTF8-encoded output string.
detectAndDecode— Recognizes and decodes QR codes.
For this lesson, I’m going to use the detectAndDecode() method to keep things quick and easy.
When you print the result, you should see something like this.
- Python packages
We have successfully generated QR codes with python.
QR codes are incredibly important in today’s modern society, where most transactions and vital data are delivered with minimum physical touch using the most technological technique. These QR codes are a valuable resource for exchanging information, conducting transactions, or just specifying the vital URL connections to a certain website.
QR code technology is highly groundbreaking, and it will persist until a superior discovery replaces it. As a result, now is an excellent moment to begin investigating this issue. If you have any questions about the different ideas discussed in this post, please leave them in the comments section below. I will do my best to respond to you as soon as possible.
We began with a basic explanation of the QR code. Following that, we used pip install to install the essential Python packages.
We then used the python-qrcode package to produce QR codes. It includes the QRCode class, which gives us a lot more power.
The QRCodeDetector class from OpenCV-Python was then used to decode QR codes. Furthermore, the most recent version has multi-detection capabilities, allowing us to identify and decode many QR codes from a single image.
In this post, we learned how simple it is to make QR codes using Python programming in less than 10 lines of code. You may build your own QR codes and decode them after installing the necessary libraries. You may insert helpful URL links or crucial information in these QR codes and share it with others in a simple, well-structured way.