A QR code is a matrix type 2D barcode (also known as checkerboard type 2D barcode), which has been frequently used on mobile devices in recent years. Compared with traditional barcodes, it can store more information. A two-dimensional code uses a certain geometric figure to record data symbol information in a black and white pattern distributed on a 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 information and other information into a square picture. In the position of the corresponding element of the matrix, the appearance of dots (square dots, dots or other shapes) is used to represent binary “1”, and the absence of dots represents binary “0”. The permutation and combination of points determine the meaning of the matrix two-dimensional bar code.
According to the standard (ISO/IEC 18004), we can understand that the QR code structure is as follows:
Each part of the two-dimensional code has its own function, and can basically be divided into two parts: functional patterns and encoding region.
Functional graphics are areas that do not participate in encoding data. It contains five modules: quiet zone, position detection patterns, separators for position detection patterns, timing patterns, and alignment patterns.
Quiet Zone
The quiet zone must be left blank. Therefore, there cannot be any patterns or markings, so as to ensure that the QR can be recognized.
Position Detection Pattern
It is used to mark the size of the QR code rectangle; three positioning patterns can be used to identify and determine the position and direction of a QR code rectangle. There are some marks in the QR code, which are located in the upper left, upper right, and lower left corners, which are used to assist the scanning software to locate the QR code and convert the coordinate system. When we scan the QR code, whether it is vertical, horizontal, or diagonal, the content can be recognized, mainly because of its credit.
Separators for Position Detection Patterns
The main function is to distinguish functional patterns and coding regions, and use white borders to distinguish positioning patterns from other areas.
Timing Patterns
Used for positioning. If the size of the QR code is too large, it will be easily distorted during scanning. The function of the timing pattern is to prevent distortion during scanning.
Alignment Patterns
Only needed in version 2 and above; the alignment mark is used to further align the coordinate system.
Format Information
It exists in all sizes and stores formatted data, such as fault tolerance level, data mask, and additional BCH fault tolerance code.
Version Information
For version 7 and above, two 3×6 areas need to be reserved to store part of the version information. The version information specifies the specifications of the QR code. There are a total of forty specifications of QR code matrix (usually black and white), from 21x21 (version 1) to 177x177 (version 40). Each version of the symbol has an increase of four modules on each side of the previous version.
Data Code
Mainly stores actual data
Error Correction Code
QR codes already have a set of international standards, so the process of drawing two-dimensional codes must be implemented in strict accordance with the standards. This process is more complicated, and the rough drawing process is summarized below. If you want to learn more about drawing details, you can read the specific standards.
Draw position detection patterns on the upper left, lower left, and upper right corners of the QR code. The position detection patterns must be a 7x7 matrix;
Draw the correction patterns, the correction patterns must be a 5x5 matrix;
Draw two positioning patterns connecting three position detection patterns;
On the basis of the above pictures, continue to draw format information;
Then draw the version information;
Fill the data code and error correction code to the QR code diagram;
Finally draw the mask pattern. Because the content is filled in the above way, a large area of blank or black block may appear, which makes scanning and recognition very difficult. Therefore, it is necessary to perform a masking operation on the entire image and the mask. The masking operation is an XOR operation. In this step, we can arrange the data into various pictures.
We often use a module myqr in the MyQR library to create QR codes, which is very simple.The reference method is “ from MyQR import myqr as mq”.
Its parameters are as follows:
Words: QR code content, link or sentence
Version: the size of the QR code, the range is [1,40], there are forty sizes of the QR code, and the size is named Version in the official document. There is a linear relationship between size and Version: Version 1 is a 21×21 matrix, and Version 2 is a 25×25 matrix. Each time you add a Version, the size will increase by four, so the linear relationship between Size and Version is 4.
Level: QR code error correction level, the range is {L,M,Q,H}, H is the highest level, default.
Picture: custom QR code background image, supported formats are .jpg, .png, .bmp, .gif, the default is black and white
Colorized: The background color of the QR code, the default is False, namely black and white
Contrast: The higher the value, the higher the contrast, the default is 1.0
Brightness: The higher the value, the higher the brightness, the default is 1.0, the value is often the same as the contrast
Save_name: QR code name, the default is qrcode.png
Save_dir: QR code path, the default is the program working path
Use the Topcoder official website to generate a QR code. You can scan the QR code with your mobile phone to directly access the Topcoder website.
The code is:
1 2 3
from MyQR import myqr as mq mq.run('https://www.topcoder.com', save_name = 'tco_qr.png')
The output is:
You can also make a custom QR code with a background picture.
The code is:
1
2
3
4
5
6
7
from MyQR
import myqr as mq
mq.run(words = 'https://www.topcoder.com',
version = 6,
picture = 'C:/Users/mcc/Desktop/tco.jpg',
colorized = True,
save_name = 'topcoder.png')
The output is:
In addition, you can also customize a QR code with a base map and a logo in the middle.
The code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import qrcode
from PIL
import Image
import matplotlib.pyplot as plt
from PIL
import ImageDraw
from PIL
import ImageFont
def getQRcode(strs, name):
qr = qrcode.QRCode(
version = 1,
error_correction = qrcode.constants.ERROR_CORRECT_L,
box_size = 10,
border = 2,
)
qr.add_data(strs)
qr.make(fit = True)
img = qr.make_image(fill_color = "black", back_color = "white")
img = img.convert("CMYK") # RGBA
icon = Image.open("tco.jpg")
img_w, img_h = img.size
factor = 6
size_w = int(img_w / factor)
size_h = int(img_h / factor)
icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w = int((img_w - icon_w) / 2)
h = int((img_h - icon_h) / 2)
img.paste(icon, (w, h), None)
img = img.convert('RGB')
img.save(name)
return img
def info(name, body):
getQRcode(body, name)
oriImg = Image.open("backgroud.jpg")
oriImg2 = Image.open(name)
oriImg2 = oriImg2.resize((490, 490))
oriImg.paste(oriImg2, (80, 80))
draw = ImageDraw.Draw(oriImg)
oriImg = oriImg.convert('RGB')
oriImg.save(name)
if __name__ == '__main__':
info("qrcode_result.png", "https://www.topcoder.com")
The output is:
https://www.iso.org/obp/ui/#iso:std:iso-iec:18004:ed-3:v1:en
https://pypi.org/project/MyQR/