Kee Kee -myqr process flow -myqr

Knowledge source network

1.MyQR file structure

qrcode
│   LICENSE.md  
│   README.md    
│   requirements.txt    #环境依赖文件
| myqr.py | └───MyQR │ │ __init__.py │ │ myqr.py #调用的文件 │ │ terminal.py #设置参数 | | │ └───mylibs │ │ __init__.pt │ │ constan.py #数据分析 | | data.py #数据编码 │ │ ECC.py #纠错编码,Error Correction Codewords | | structure.py #数据结构 | | matrix.py #获得QR矩阵 | | draw.py #生成二维码 | | theqrmodule.py #结合函数 │ └───example │ 0.png │ 1.png | 2.png | ... 

2. The two-dimensional code generating step

2.1 Data AnalysisMyQR/mylibs/constan.py

Determining the type of encoded characters, converted into a symbol character according to the corresponding character set.

2.2 Data encodingMyQR/mylibs/data.py

The bit stream is converted to data characters, a code word for each eight, the overall configuration of a data code word sequence.

2.3 error correction codingMyQR/mylibs/ECC.py

As required to the above block codeword sequence, and error correction level and according to the codeword block, and generating an error correction code word, and the error correction code word after the words added to the data symbol sequence, to become a new sequence.

2.4 The final construction dataMyQR/mylibs/structure.py + matrix.py

Under conditions determined specifications, in order, the sequence generated above into the block, the matrix of data into two-dimensional code can be drawn.

Create a two-dimensional matrix code

# MyQR/mylibs/matrix.py
def get_qrmatrix(ver, ecl, bits): num = (ver - 1) * 4 + 21 qrmatrix = [[None] * num for i in range(num)] # 添加查找器模式和添加分隔符 add_finder_and_separator(qrmatrix) # 添加校准模式 add_alignment(ver, qrmatrix) # 添加时间模式 add_timing(qrmatrix) # 添加涂黑模块和保留区域 add_dark_and_reserving(ver, qrmatrix) maskmatrix = [i[:] for i in qrmatrix] # 放置数据位 place_bits(bits, qrmatrix) # 蒙版操作 mask_num, qrmatrix = mask(maskmatrix, qrmatrix) # 格式信息 add_format_and_version_string(ver, ecl, mask_num, qrmatrix) return qrmatrix 

2.5 generate two-dimensional codeMyQR/mylibs/draw.py

Use  draw.py draw two-dimensional code.

def draw_qrcode(abspath, qrmatrix): unit_len = 3 x = y = 4*unit_len pic = Image.new('1', [(len(qrmatrix)+8)*unit_len]*2, 'white') #新建一张白色的底图 ''' 循环矩阵中的单位,在需要涂黑的单位启用dra_a_black_unit()函数涂黑。 ''' for line in qrmatrix: for module in line: if module: draw_a_black_unit(pic, x, y, unit_len) #画出黑单位 x += unit_len x, y = 4*unit_len, y+unit_len saving = os.path.join(abspath, 'qrcode.png') pic.save(saving) # 保存二维码图片 return saving 

3. The principle of the combined picture

Let's look at  /MyQR/myqr.py the  combine() method that calls the  Pillow library

Read image manipulation

    qr = Image.open(qr_name)    #读取二维码图片
    qr = qr.convert('RGBA') if colorized else qr #判断二维码是否有色 bg0 = Image.open(bg_name).convert('RGBA') #读取要合并的图片 bg0 = ImageEnhance.Contrast(bg0).enhance(contrast) # 调节对比度 bg0 = ImageEnhance.Brightness(bg0).enhance(brightness) # 调节亮度 

The newly added images covering the original two-dimensional code images to generate a new image and save it.

    for i in range(qr.size[0]-24): for j in range(qr.size[1]-24): if not ((i in (18,19,20)) or (j in (18,19,20)) or (i<24 and j<24) or (i<24 and j>qr.size[1]-49) or (i>qr.size[0]-49 and j<24) or ((i,j) in aligs) or (i%3==1 and j%3==1) or (bg0.getpixel((i,j))[3]==0)): qr.putpixel((i+12,j+12), bg.getpixel((i,j)))

Knowledge source network

1.MyQR file structure

qrcode
│   LICENSE.md  
│   README.md    
│   requirements.txt    #环境依赖文件
| myqr.py | └───MyQR │ │ __init__.py │ │ myqr.py #调用的文件 │ │ terminal.py #设置参数 | | │ └───mylibs │ │ __init__.pt │ │ constan.py #数据分析 | | data.py #数据编码 │ │ ECC.py #纠错编码,Error Correction Codewords | | structure.py #数据结构 | | matrix.py #获得QR矩阵 | | draw.py #生成二维码 | | theqrmodule.py #结合函数 │ └───example │ 0.png │ 1.png | 2.png | ... 

2. The two-dimensional code generating step

2.1 Data AnalysisMyQR/mylibs/constan.py

Determining the type of encoded characters, converted into a symbol character according to the corresponding character set.

2.2 Data encodingMyQR/mylibs/data.py

The bit stream is converted to data characters, a code word for each eight, the overall configuration of a data code word sequence.

2.3 error correction codingMyQR/mylibs/ECC.py

As required to the above block codeword sequence, and error correction level and according to the codeword block, and generating an error correction code word, and the error correction code word after the words added to the data symbol sequence, to become a new sequence.

2.4 The final construction dataMyQR/mylibs/structure.py + matrix.py

Under conditions determined specifications, in order, the sequence generated above into the block, the matrix of data into two-dimensional code can be drawn.

Create a two-dimensional matrix code

# MyQR/mylibs/matrix.py
def get_qrmatrix(ver, ecl, bits): num = (ver - 1) * 4 + 21 qrmatrix = [[None] * num for i in range(num)] # 添加查找器模式和添加分隔符 add_finder_and_separator(qrmatrix) # 添加校准模式 add_alignment(ver, qrmatrix) # 添加时间模式 add_timing(qrmatrix) # 添加涂黑模块和保留区域 add_dark_and_reserving(ver, qrmatrix) maskmatrix = [i[:] for i in qrmatrix] # 放置数据位 place_bits(bits, qrmatrix) # 蒙版操作 mask_num, qrmatrix = mask(maskmatrix, qrmatrix) # 格式信息 add_format_and_version_string(ver, ecl, mask_num, qrmatrix) return qrmatrix 

2.5 generate two-dimensional codeMyQR/mylibs/draw.py

Use  draw.py draw two-dimensional code.

def draw_qrcode(abspath, qrmatrix): unit_len = 3 x = y = 4*unit_len pic = Image.new('1', [(len(qrmatrix)+8)*unit_len]*2, 'white') #新建一张白色的底图 ''' 循环矩阵中的单位,在需要涂黑的单位启用dra_a_black_unit()函数涂黑。 ''' for line in qrmatrix: for module in line: if module: draw_a_black_unit(pic, x, y, unit_len) #画出黑单位 x += unit_len x, y = 4*unit_len, y+unit_len saving = os.path.join(abspath, 'qrcode.png') pic.save(saving) # 保存二维码图片 return saving 

3. The principle of the combined picture

Let's look at  /MyQR/myqr.py the  combine() method that calls the  Pillow library

Read image manipulation

    qr = Image.open(qr_name)    #读取二维码图片
    qr = qr.convert('RGBA') if colorized else qr #判断二维码是否有色 bg0 = Image.open(bg_name).convert('RGBA') #读取要合并的图片 bg0 = ImageEnhance.Contrast(bg0).enhance(contrast) # 调节对比度 bg0 = ImageEnhance.Brightness(bg0).enhance(brightness) # 调节亮度 

The newly added images covering the original two-dimensional code images to generate a new image and save it.

    for i in range(qr.size[0]-24): for j in range(qr.size[1]-24): if not ((i in (18,19,20)) or (j in (18,19,20)) or (i<24 and j<24) or (i<24 and j>qr.size[1]-49) or (i>qr.size[0]-49 and j<24) or ((i,j) in aligs) or (i%3==1 and j%3==1) or (bg0.getpixel((i,j))[3]==0)): qr.putpixel((i+12,j+12), bg.getpixel((i,j)))

Guess you like

Origin www.cnblogs.com/dgfdgdf2290/p/11701630.html