Using Python to generate two-dimensional code

Source: Internet
Author: User

In a new era, people believe that the pursuit of fresh, natural will be the birth of new inventions. Last year, during the "Belt and Road" International cooperation summit, 20 young people voted to elect China's "Four New Inventions": High-speed rail, sweep-code payment, bicycle-sharing and online shopping. Where scan code payment refers to the mobile phone by scanning the QR code to jump to the payment page, and then make the payment. This new payment method, create the two-dimensional code flying phenomenon. So let's take a steak. How to use Python to generate a QR code image.

12-dimensional Code

A QR code (2-dimensional bar code) is a black-and-white graphic that distributes data symbols in a plane (two-dimensional direction) by a certain geometry. It can record numbers, English letters, Chinese characters, Japanese letters, special symbols (such as spaces,%,/, etc.), and binary information into a square picture.

Therefore, in the process of conversion, it is inseparable from the encoding compression method. In many kinds of two-dimensional barcode, the commonly used code system is: Data Matrix, Maxi code, Aztec, QR Code, Vericode, PDF417, Ultracode, code, code 16K, and so on.

The application of QR code in real life is more and more common, which is attributed to the popularity of the code system. We often say that the QR code is it. So, the QR code is also known as the barcode.

QR code is a matrix-type two-dimensional barcode (also known as checkerboard two-dimensional barcode). 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 element of the matrix, the occurrence of the point (square point, dot, or other shape) represents the binary "1", the point does not appear to represent the binary "0", the arrangement of the points to determine the matrix of two-dimensional bar code represents the meaning .

22-Dimensional Code structure

Our goal is to use Python to generate QR codes, so we need to understand the structure of QR codes first.

1) Functional Graphics
functional graphs are areas that do not participate in coded data. It contains the blank area , position detection graphics , position detection graphics separator , positioning graphics , correction graphics five modules.

    • Blank area

The white space is the name to leave blank. Therefore, there cannot be any pattern or mark here. This will ensure that the QR can be identified.

    • Position Detection Graphics

This is somewhat similar to the Chinese "back" word. In the QR code, there is an identifier, which is the upper left, upper right, and lower left corner respectively. The function is to assist scanning software to locate the QR code and convert the coordinate system. We scan the QR code, whether it is vertical sweep, transverse sweep, oblique sweep can identify the content, mainly its credit.

    • Position Probe Graphic Separator

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

    • Positioning graphics

It consists of the individual lines of the black and white intervals. Primarily used to indicate the identity density and to determine the coordinate system. The reason is that there are 40 versions of the QR code, meaning there are 40 sizes. The larger the size of each two-dimensional code, the farther away the scan will be.

    • Correcting graphics

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

2) Coding Area
The coded area is the area where data is stored for encoding. It is composed of the format information , the version information , the data and the error correcting code word three parts.

    • Format information

This information is available for all dimensions of the QR code. It holds information about formatted data, such as fault tolerance levels, data masks, and additional self-BCH fault tolerance codes.

    • Version information

The version information is the specification of the two-dimensional code. In front of the QR code a total of 40 specifications of the matrix (generally black and white), from 21x21 (version 1), to 177x177 (version 40), each version of the symbol is more than the previous version of 4 modules per side.

    • Data and error correction codes

The main is to store the actual data and for error correction code word.

32-dimensional Code drawing process

QR code is already a set of international standards, the process of drawing two-dimensional code strictly in accordance with the standard to execute. This process is more complex, I also looked at the approximate, and then summed up the approximate drawing process. If you want to learn more about drawing details, you can read the standard.

The approximate process of drawing the QR code is as follows:
1) in the two-dimensional code, the upper left corner, the lower left corner, the upper right corner of the drawing position detection graphics . The position detection graph must be a 7x7 matrix.
2) Draw the correction graphic . The correction graph must be a 5x5 matrix.
3) Draw two positions to connect the three position detection graphics .
4) on the basis of the above image, continue to draw the format information .
5) Then draw the version information .
6) Fill in the data code and error correction code to the two-dimensional code map.
7) Finally, the case of the Mongolian layout is plotted. Because the content is populated as described above, a large area of white space or black blocks can occur, which can make scanning recognition difficult. So you need to mask the entire image and mask operation (masking), the mask operation is XOR or XOR operation. In this step, we can arrange the data into various pictures.

42-Dimensional code generation

Now that we know the QR code principle, we can use Python to generate a QR code. However, the network of tall man abound. The great God has written a third-party library of Python-generated QR codes, so we don't need to reinvent the wheel and use a ready-made library.

I would recommend two libraries:QRCode and python-qrcode.

    • QRCode

QRCode running on the Python 3 version, it can play a lot of tricks. For example, can generate the following three kinds of two-dimensional code image: Ordinary two-dimensional code, with pictures of the art of two-dimensional code (black and white and color), dynamic two-dimensional code (black and white and color). It is more suitable for scenes that are directly used to generate a two-dimensional code picture.

You can use the Pip method to install the QRCode library. However, the library relies on pillow,numpy , and ImageIO. Therefore, we need to install the dependent libraries before installing QRCode. The final installation commands are as follows:

# Install PIP install pillowpip install numpypip install IMAGEIOPIP install MYQR

The library to create a picture with the art of two-dimensional code is a big bright spot, the specific usage is as follows:

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

The purpose of the above command is to write the GitHub home page into a color QR code.

The library also supports the generation of GIF color two-dimensional code images, as follows:

MYQR https://github.com-p github.gif-c-con 1.5-bri 1.6

The last Github address for this library: Https://github.com/sylnsfar/qrcode

    • Python-qrcode

Python-qrcode A little bit less than qrcode . But it also has its own characteristics. It supports generating vectors, and is more suitable for scenes that generate QR codes in code.

Installation Python-qrcode also recommends the use of the Pip method, the installation command is as follows:

Pip Install QRCode

In Python code, the simplest usage is this.

Import qrcodeimg = Qrcode.make (' https://github.com ')

It also supports custom QR codes with the following details:

Import QRCODEQR = 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")

  

Using Python to generate two-dimensional code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.