Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate QR Code in Python

2025-02-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to generate QR codes in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1 QR code

Two-dimensional code (2-dimensional bar code) records data symbol information with black-and-white graphics distributed in the plane (two-dimensional direction) according to a certain rule. It can record numbers, English letters, Chinese characters, Japanese letters, special symbols (such as spaces,%, /, etc.), binary and other information into a square picture.

Therefore, in the process of conversion, coding compression is inseparable. In many kinds of two-dimensional bar codes, the commonly used codes are: Data Matrix, Maxi Code, Aztec, QR Code, Vericode, PDF417, Ultracode, Code 49, Code 16K and so on.

The application of QR codes in real life is becoming more and more common, thanks to the popularity of the QR code code system. The QR code we often talk about is it. Therefore, the QR code is also called QR code.

QR code is a kind of matrix two-dimensional bar code (also known as checkerboard two-dimensional bar code). It is encoded in a rectangular space by the different distributions of black and white pixels in the matrix. In the position of the corresponding elements of the matrix, the binary "1" is represented by the appearance of points (square dots, dots or other shapes), and the absence of dots represents the binary "0". The permutation and combination of points determines the meaning represented by the matrix two-dimensional bar code.

2 QR code structure

Our aim is to use Python to generate QR codes, so we need to understand the structure of QR codes (QR codes) first. According to the standard (ISO/IEC 18004), we can see that the structure of the QR code is as follows:

Picture source network

1) functional graphics

Functional graphics are areas that do not participate in encoding data. It includes five modules: blank area, position detection pattern, position detection pattern separator, positioning figure and correction pattern.

Blank area

The blank area, as the name implies, is to leave blank space. Therefore, there cannot be any patterns or marks here. This is the only way to ensure that QR can be identified.

Position detection pattern

This is a bit similar to the Chinese word "Hui". There is a logo in the QR code, which is in the upper-left, upper-right and lower-left corners, respectively. The function is to assist the scanning software to locate the QR code and transform the coordinate system. When we scan the QR code, whether it is scanning vertically, horizontally or diagonally, we can identify the content, mainly because of its credit.

Position detection pattern separator

The main function is to distinguish between functional graphics and coding areas.

Positioning graphics

It consists of separate lines separated by black and white. It is mainly used to indicate the identification density and determine the coordinate system. The reason is that there are 40 versions of QR code, which means there are 40 sizes. The larger the size of each QR code, the longer the scanning distance.

Correction figure

Only QR codes with Version 2 and above have correction marks. The correction identification is used to further correct the coordinate system.

2) coding region

The coding area is the area where the data is encoded and stored. It consists of format information, version information, data and error correction codeword.

Format information

This information is available in QR codes of all sizes. It stores some information about formatting data, such as fault tolerance level, data mask, and additional self-BCH fault tolerance code.

Version information

The version information is the specification of the QR code. As mentioned earlier, QR code has a total of 40 specifications of matrix (usually black and white), from 21x21 (version 1) to 177x177 (version 40), each version symbol adds 4 modules on each side than the previous version.

Data and error correction codes

It mainly stores the actual data and is used for error correction codewords.

3 the drawing process of QR code

QR codes already have a set of international standards, and the process of drawing QR codes is strictly carried out in accordance with the standards. This process is quite complicated, I also looked at the outline, and then summed up the general drawing process. If you want to learn more about the details of the drawing, you can read the standards.

The process of drawing the QR code is as follows:

1) draw position detection graphics in the upper left corner, lower left corner and upper right corner of the QR code. The location detection pattern must be a matrix of 7x7.

2) draw the correction figure. The correction graph must be a 5x5 matrix.

3) draw two positioning graphs connecting three position detection graphics.

4) continue to draw format information on the basis of the above pictures.

5) then draw the version information.

6) fill the data code and error correction code into the two-dimensional code diagram.

7) finally, draw the mask pattern. Because the content is filled in the above way, there may be a large area of blank or black blocks, which makes it very difficult to scan and identify. Therefore, the whole image and the mask need to be masked (Masking), and the mask operation is XOR XOR operation. In this step, we can arrange the data into various pictures.

4 generation of QR code

Now that we know the principle of QR code, we can use Python to generate QR code. However, there are many talented people on the Internet. A great god has written a third-party library for Python to generate QR codes, so we don't need to remake wheels, just use off-the-shelf libraries.

I recommend two libraries: qrcode and python-qrcode.

Qrcode

Qrcode runs on Python 3, and it can do a lot of tricks. For example, it can generate three kinds of QR code pictures: ordinary QR code, artistic QR code with picture (black and white and color), and dynamic QR code (black and white and color). It is more suitable for scenarios that are directly used to generate QR code pictures.

You can use pip to install the qrcode library. But the library relies on pillow, numpy, and imageio. Therefore, we need to install the dependent library before installing qrcode. The final installation command is as follows:

# install one by one

Pip install pillow

Pip install numpy

Pip install imageio

Pip install myqr

The generation of art QR codes with images in this library is a highlight, and the specific uses are as follows:

Myqr https://github.com-p github.jpg-c

Python-qrcode

Python-qrcode is slightly inferior to qrcode. But it also has its own characteristics. It supports the generation of vector graphics and is more suitable for scenarios where QR codes are generated in code.

It is also recommended to use pip to install python-qrcode. The installation command is as follows:

Pip install qrcode

The simplest use in Python code is like this.

Import qrcode

Img = qrcode.make ('https://github.com')

It also supports custom QR code information, which can be used as follows:

Import qrcode

Qr = qrcode.QRCode (

Version=1

Error_correction=qrcode.constants.ERROR_CORRECT_L

Box_size=10

Border=4

)

Qr.add_data ('https://github.com')

Qr.make (fit=True)

Img = qr.make_image (fill_color= "black", back_color= "white")

After reading the above, do you have any further understanding of how to generate QR codes in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report