Qiao generate two-dimensional code in Python Qrcode library Daquan

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/suoyue_py/article/details/98486348

Introduction of two-dimensional code

QR Code is a two-dimensional barcode, QR acronym from the English "Quick Response", i.e. rapid response means, the QR code can be derived from the inventors want their content is quickly decoded.
QR code than ordinary bar code that can be stored richer information, including text, URL addresses, and other types of data encryption, also do not need to be like ordinary bar code scanning to be like when the aligners scanner.
QR code square, only black and white. In three corners of the printed smaller "return" square pattern, the three decoding software help positioning pattern, the user can scan need not be aligned at any angle data can be read correctly.
Here Insert Picture Description

QR code is a two-dimensional structure

  • Version Information
    version1 (21 * 21), version2 , ..., version40 (177 * 177), a total of 40 versions
    version on behalf of each line how many symbols modules, each version increase over the previous version 4 symbols modules, calculate formula is (n-1) * 4 + 21, each symbol module storing a binary 0 or 1. Black modules representing a binary "1" , white modules representing a binary "0" , for example, each line 21 represents version1 symbols modules.
  • Format information
    stored fault tolerance level L (7%), M (15%), Q (25%), R & lt (35%)
    L level of 7% of the codeword can be corrected
    M level of 15% of the codeword can be corrected
    Q level 25% of the codeword can be corrected
    H level of 30% of the codeword can be corrected
    fault tolerant: the two-dimensional information allows the duplication of the stored portion, the higher the level, the higher the proportion of repeating information.
    Objective: To even partially hidden two-dimensional code (such as icons, etc.), and they can get all the contents of the two-dimensional code
  • Data and error correction code word
    is actually stored two-dimensional code information, and an error correction code words (an error correction for the damage caused by two-dimensional code, the two-dimensional code that is to say when the partially hidden by the error correction code word to retrieve)
  • Position detection pattern
    for two-dimensional code is positioned . Position detection pattern for marking the size of the rectangle, a rectangle determining pattern 3

qrcode two-dimensional code generation

Learn Python code is automatically generated by qrcode two-dimensional code, no longer have to look for forage : https: //cli.im/ to generate two-dimensional code loaded to force the
Here Insert Picture Description
preparatory work:
the need to use qrcode PIL libraries and library, this need not install install some
installation qrcode: command line window enter pip install qrcode
Here Insert Picture Description
install PIL library: PIP Pillow install
Here Insert Picture Description
this code can start knocking
Here Insert Picture Description
the two-dimensional code generation need only three lines of code:

import qrcode		#导入qrcode库
img = qrcode.make("https://blog.csdn.net/suoyue_py")		#生成二维码的网址(文本也行)
img.save("suoyue.png")		#生成二维码后保存的图片名

Here pit: code is stored and can not be "qrcode.py" when naming files, because import is equivalent to their import, and it must be modified for non "qrcode.py" (to capital letter Q will do ~~) .
Otherwise it will error Runtime:
AttributeError: 'Module' Object attribute has NO 'the make'
Here Insert Picture Description
Note: Save two-dimensional code (picture) need to call PIL library, so the library will need to install pillow, otherwise also will complain
after normal operation can solve minor bug find the two-dimensional code generated in the same folder

Premium: generating two-dimensional code with the logo

import qrcode
from PIL import Image
import os,sys

#参数 string:二维码字符串;path:生成的二维码保存路径;logo:logo文件路径
def gen_qrcode(string,path,logo=""):
    qr = qrcode.QRCode(
        version=2,      #控制二维码的大小:(25*25)
        error_correction=qrcode.constants.ERROR_CORRECT_H,  #ERROR_CORRECT_H:30%的字码可被容错
        box_size=8,     #控制二维码中每个小格子包含的像素数
        border=1        #控制边框(二维码与图片边界的距离)包含的格子数(默认为4) 
    )
    qr.add_data(string)
    qr.make(fit=True)
    img = qr.make_image()
    img = img.convert("RGBA")
    if logo and os.path.exists(logo):
        try:
            icon = Image.open(logo)
            img_w, img_h = img.size
        except Exception as e:
            print(e)
            sys.exit(1)
        factor = 4
        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)
        icon = icon.convert("RGBA")
        img.paste(icon,(w,h),icon)
    img.save(path)
    #调用系统命令打开图片
    # xdg-open :在用户的首选应用程序中打开文件或url
    os.system('xdg-open %s' % path)

if __name__ == "__main__":
    info = "https://blog.csdn.net/suoyue_py"        #生成二维码的网址
    pic_path = "qrsuoyue.png"           #生成二维码保存的图片名
    logo_path = "logo.png"              #生成二维码所用的图标
    gen_qrcode(info,pic_path,logo_path)     #调用函数

error_correction: control error correction function of two-dimensional code.
The following values may be four constants:
1.ERROR_CORRECT_ L :. 7% of the word may be a fault-tolerant
2.ERROR_CORRECT_ M (default): 15% of the word may be a fault-tolerant
3.ERROR_CORRECT_ Q : 25% of the word may be a fault-tolerant
4.ERROR_CORRECT_ H : 30% of the word may be a fault-tolerant
fault tolerant coefficients of two-dimensional code (error_correction referred to above), the higher the generated two-dimensional code may be incomplete allowing greater rate, two-dimensional code and data of the main save on the four corners of the picture, so put a small icon in the middle of the two-dimensional code, the identification of the two-dimensional code is not much impact

Guess you like

Origin blog.csdn.net/suoyue_py/article/details/98486348