Machine Learning

How to generate QR codes in Python

in QR codes

The “QR” in QR Code stands for quick response. QR codes are an emerging and easy way to find information without having to type or search for anything on your phone. These codes are actually a pattern of black and white scenes, with structured digital codes embedded in them. These days, restaurants use QR codes to present the menu to their customers, stores and marts use them as a form of digital payments without any hassle, event management teams use them as instant calls to their events and conferences, and more.

It creates QR codes

As mentioned earlier, QR codes are developed as a pattern or grid of small black and white squares that store information in binary form, that is, as 0s and 0s. Details are encoded within the code with special provisions for customizing colors, backgrounds, and frames, as long as the pattern remains the same.

The following is an example of a QR code created with the Python “QRCode” package:

Link to my TDS author profile (photo by Author)

Scanning the above code with a scanner with QR code scanning capabilities will take you to my TDS profile, where you can easily access Python projects to start with.

In this article, we will go through the Python package qrcodeInstalling and testing its different functions to design QR codes.

Installing the package

Before starting the project, we will install the relevant packages. I am using Pycharm Ide for this project. To enter “QRCode” Python Package, I'll go to the Python forum and type the following:

pip install qrcode
Entering a QRCode package (image by Author)

Installing the QRCode package will allow us to create QR codes as PNG files and render them to the Python Console. However, if we need functionality related to multiple images, we have to install Pillow A powerful image processing package.

pip install "qrcode[pil]"
Entering a QRCode package with Pillow (photo by Author)

Importing the library and generating a simple QR code

Now we will start coding. First, we will import the library and enter the alias name at our ease. As can be seen in the QR Code Python documentation, an image of a QR code URL can be easily generated and saved as a PNG using the following lines of code:

import qrcode as qr
img = qr.make("
img.save("Python QR Code Documentaion.png")
Python QR Code CODE CODE (Image by the author)

This is a simple QR code made using make() run and saved as a PNG file using save() work.

Advanced performance

For advanced image processing features, we will use the QRCode section in qrcode A python package.

Classes and objects are a useful concept in programming. Python classes provide BlealPrint color for creating objects that share common properties, attributes (variables), and methods (functions). We will use the Python Class constructor to create QR codes as objects of that class.

Below is the general code that allows us to create and save QR codes:

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data('
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
img.save("Understanding Classes and Objects.png")
Understanding classes and objects in Python (image by Author)

The code above created and saved the QR code as a PNG. If you scan the code above, you'll land on a lovely Python tutorial that explores how the concepts of Python classes and objects apply to real-world projects.

Now, let's dive deeper into the general code above and examine the functions and play with their differences.

The Creation of a Thing

The first line of code after import qrcode Library constructs an object, with its attributes and methods enclosed inside round brackets.

qr = qrcode.QRCode(
    ...
    ...
    ...
    ...
    ...
)

Here, qrcode referred to in the Python package as well QRCode() referred to in the classroom.

To define a version

Next is to define the version. We can set the value from 1 to 40, and this will result in a different size of the QR code.

import qrcode
qr = qrcode.QRCode(
    version=1,
    ...
    ...
    ...
    ...
)

Let's create a QR code with version variable is set to 5.

Version version set to 5 (image by Author)

Now let's plan version parameter to 15:

Version version set to 15 (image by Author)

As can be seen from the two QR codes above, version attribute determines the size of the QR code. Very little version 1 Outputting the QR code as a 21 × 21 grid.

Error correction

The next attribute we will go through error_correction. The error correction features are concerned with updating the data, so that even if the QR code is damaged, it will still be scanned. There are 4 different levels of error_correction:

  • Correct error L: This provides the lowest level of error correction, which is 7%
  • Correct error M: This provides a moderate level of data correction, 15% or less, thus providing a balance between error correction and data size
  • Correct error Q: This provides 25% or less error correction
  • Correct error h: This provides a high level of error correction and is suitable for applications that can be highly damaging, and where data size is not a concern.
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    ...
    ...
)

A lot of percentages error_correctionwhere it is easy to scan, or where parts of them are damaged. On the flip side, QR codes have become larger in size, and this poses a problem where data compression is a major requirement.

Let's create unique QR codes error_correction species.

Error correction type l (photo by Author)
Error correction type m (photo by Author)
Error correction type Q (photo by Author)
Error correction type h (photo by Author)

As can be seen from the variety of QR codes generated above, such as error_correction Increasingly, the complexity of QR Codes increases, which means that the data size increases, while the data redundancy also increases.

Box size

The next feature we will explore and play with box_size. This page box_size In Python qrcode Library refers to the number of pixels the QR code will have.

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    ...
)

Let's see how our QR code changes with different values ​​of box_size:

Box size = 1 (image by Author)
Box size = 100 (image by Author)
Box_size variable (image by Author)

The images above show the difference when the pixel values ​​change, although it may not be noticeable to the naked eye if the difference is small.

The border

The last attribute we need to define to create an object is this border. This means in the surrounding box around the code. The minimum value is 4, and we can make as many as we like. Let's see how the code changes with the change in this attribute:

Border = 4 (image by Author)
Border = 10 (image by Author)

We can see the difference in the border of the images above, which can be easily done with border Variable.

Adding Data

When our object is created, with certain values ​​of version, error_correction type, box_size and bordernow we will enter the data that needs to be entered into the QR code. This data can be string, URL, location, contact information, etc. This can be easily done with add_data() method of this section.

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=1,
    border=10,
)
qr.add_data('
qr.make(fit=True)

The last line of code above calls qr.make(fit=True). This automatically matches the QR code to the given data, after analyzing it. It uses the smallest QR code to accept the information, and it does not require us to manually perform the good qualities.

An image object is created

When the QR code object is created, we will use PIL to generate an image while defining its colors, and save that image in a suitable format. For the next example, we will set the background to black and the fill color to Pink in our QR code, as can be seen in the code below:

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=1,
    border=10,
)
qr.add_data('
qr.make(fit=True)

img = qr.make_image(fill_color="pink", back_color="black")
img.save("calc.png")
Color coding of QR code (Author's IMAEE)

Lasting

In this article, we explored the Python QR code package, which includes a Blueprint for creating QR codes and saving them in different file formats. It includes advanced customization of QR code generation, which we went through with the understanding of classes and objects. This was a simple and beginner's Python tutorial that required some high-level knowledge of classes and objects.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button