 When it comes to two-dimensional code, we must be familiar with it , It can be said that the QR code is almost *** To every corner of our lives , For example , We scan the QR code to pay when we go shopping in the supermarket , When we go out, we take the bus and subway to scan the QR code and enter the station , When we go to rookie station to pick up, we scan the QR code to pick up , If one day we meet an interesting person , Maybe wechat will scan the QR code and add a friend , If you read our article and feel like it helps you a little bit , You can also scan the end of the code to pay attention to our official account .

We go through Python As well as the need to generate two-dimensional identification code library ：qrcode、myqr、zxing, Installation through `pip install qrcode/myqr/zxing` that will do .

## Two dimensional code structure

First , Let's take a brief look at the structure of QR code , As shown in the figure below ： From the figure, we can see that the overall structure of the two-dimensional code is divided into two parts: function graphics and coding area , Functional graphics are subdivided into ： Blank area 、 Position detection pattern 、 Position detection graphic separator 、 Positioning graphics 、 Correct the figure , And the coding area is subdivided into ： Format information 、 Version information 、 Data and error correcting codewords , Let's take a brief look at the functions of each part ：

• Blank area ： message , There is no need to do anything about

• Position detection pattern ： Help scanning software locate QR code

• Position detection graphic separator ： Distinguish between functional graphics and coding area

• Positioning graphics ： Indicating the density of marks and determining the coordinate system

• Correct the figure ： The number and position of correction figures

• Format information ： Information that stores formatted data

• Version information ： The specifications of the QR code , Two dimensional code symbols share 40 A matrix of two sizes

• Data and error correcting codewords ： The two-dimensional code information and error correction code words actually saved （ Used to correct the error caused by QR code corruption ）

## Generate qr code

To create a QR code, we can use  qrcode and myqr Two Python library , Let's take a look at it in detail .

### qrcode

We may see some QR codes in our life , After we scanned, we jumped to an address , This function qrcode Can be realized , Take a look at an example ：

``import qrcode``# QR code content （ Link address or text ）``data = 'https://www.baidu.com/'``# Generate qr code ``img = qrcode.make(data=data)``# Display QR code ``img.show()``# Save QR code ``# img.save('qr.jpg')``

We can scan the above two-dimensional code to jump to the address we set .

The above two-dimensional code is more primitive , We can also simply set it up and beautify it , The code implementation is as follows ：

``import qrcode``'''``version： The size of the lattice matrix of the QR code , It can be 1 To 40,1 The minimum is 21*21,40 yes 177*177``error_correction： Error tolerance rate of QR code , Default ERROR_CORRECT_M, Allow less than 15% Error rate ``box_size： The number of pixels in each cell of QR code ``border： The number of squares from the QR code to the frame of the picture , The default value is 4``'''``qr = qrcode.QRCode(``    version=2,``    error_correction=qrcode.constants.ERROR_CORRECT_L,``    box_size=15,``    border=3,``)``# QR code content ``data = 'https://www.baidu.com/'``qr.add_data(data=data)``# Enable QR code color settings ``qr.make(fit=True)``img = qr.make_image(fill_color='blue', back_color='white')``# Display QR code ``img.show()``

### myqr

If we are not satisfied with the style of QR code generated above , We need to use myqr The library , It can not only generate ordinary two-dimensional code , Can also generate two-dimensional code with pictures and dynamic two-dimensional code , But when we want to see the generated QR code , You need to go to the save location and open it manually .

#### Ordinary QR code

So let's use myqr To generate a normal QR code , Take a look at an example ：

``from MyQR import myqr``'''``words： Content ``version： Fault tolerance ``save_name： Saved name ``'''``myqr.run(words='https://www.baidu.com/',``         version=1,``        save_name='myqr.png')``

#### Two dimensional code with graph

We then use myqr To generate a QR code with a graph , Take a look at an example ：

``from MyQR import myqr``'''``picture： The image used to generate the QR code ``colorized：False For black and white ,True For color ``'''``myqr.run(words='https://www.baidu.com/',``         version=1,``         picture='bg.jpg',``         colorized=True,``         save_name='pmyqr.png')``

#### Dynamic QR code

Last , We use myqr To generate a dynamic QR code , Take a look at an example ：

``from MyQR import myqr``myqr.run(words='https://www.baidu.com/',``         version=1,``         picture='my.gif',``         colorized=True,``         save_name='myqr.gif')``

## Analysis of QR code

We go through zxing Library to identify the QR code , It's easy to use , Let's take an example of ：

``import zxing``reader = zxing.BarCodeReader()``barcode = reader.decode('myqr.gif')``print(barcode.parsed)``

Execution results ：

`https://www.baidu.com/`

Of course, we can also beautify the existing two-dimensional code , The way of implementation is ：

• Analyze the content of the existing QR code

• Use the parsed content to generate a new QR code

Take our official account as an example , As shown in the figure below ：

Take a look at the implementation code ：

``import zxing``from MyQR import myqr``reader = zxing.BarCodeReader()``barcode = reader.decode('gzh.jpg')``myqr.run(words=str(barcode.parsed),``         version=1,``         picture='my.gif',``         colorized=True,``  save_name='gmyqr.gif')``

Take a look at the effect ：

Now the two-dimensional code on the scan will jump to our official account .

## summary

In this article we show you how to use Python To generate and identify the QR code , Through the study of this article , We can use some static pictures or GIF To make some specific QR code files .