Python combat: Use qrcode generate two-dimensional code

Foreword

Now, life used to the scene two-dimensional code can be said everywhere. Especially mobile payment, the payment page jump to mobile phones to scan two-dimensional code, I believe we are no strangers to this scenario.

The most commonly used to our daily two-dimensional code is a two-dimensional matrix barcode (also known as two-dimensional bar code chessboard). It is a rectangular space through the black and white pixels is encoded in different matrix distributions. Matrix position corresponding element appears with the point (square dots, dots or other shape) represents a binary "1", the dot represents a binary "0" does not appear, permutations points determined by matrix-dimensional bar code What it means.

So let's Pa Pa how to use Python to generate a two-dimensional code images.

A simple two-dimensional code generation

  1. Installation qrcode library
pip install qrcode 

Because of the domestic network speed, and sometimes download times out, it is recommended to use domestic sources of pip

pip install qrcode -i https://pypi.douban.com/simple

qrcode rely Image package, so they will have to install a package, as Suman, recommended the use of domestic sources of pip

pip install Image -i https://pypi.douban.com/simple
  1. Generate a simple two-dimensional code

After installation, it generates a simple two-dimensional code to try. (I use the blog Home addresses secretly generate a two-dimensional code [a head] [a head])

data = "https://blog.csdn.net/m1090760001"
img = qrcode.make(data)
img.show()
img.save("qr_code.png")

Try running about the effect
Here Insert Picture Description

  1. Usually the step of generating two-dimensional code:
  • Creating objects QRCode
  • add_data is () to add data
  • make_image () to create a two-dimensional code (type of return im Picture Object)
  • Automatically open the picture, im.show ()

The above case, we only use img = qrcode.make (data) on the realization of a two-dimensional code generation, because qrcode provide a simple call interface. In fact, you can see the view source code:

def make(data=None, **kwargs):
    qr = QRCode(**kwargs)
    qr.add_data(data)
    return qr.make_image()

Second, the custom two-dimensional code

If you want to generate a more complex two-dimensional code, you must know how to set the relevant properties.

  1. The basic parameters:
  • version: an integer ranging from 1 to 40, represents the size of the two-dimensional code (the minimum value is 1, is a matrix of 12 × 12), if you want True parameters automatically generated, and the value to use fit = None i.e. can. (Default is automatically generated)
  • error_correction: two-dimensional code is an error correction range may be selected four constants:
Attributes description
ERROR_CORRECT_L 7% of the error will be corrected
ERROR_CORRECT_M 15% of the errors will be corrected (default)
ERROR_CORRECT_Q 25% or less of the error will be corrected
ERROR_CORRECT_H 30% of the errors will be corrected
  • The number of pixels per dot (block) of: boxsize
  • border: two-dimensional code from the image frame from the periphery, the default is 4, and the relevant provisions of a minimum of 4
  1. Setting the color of the grid is filled, the background color setting two-dimensional code
# 设置红色的填充格,背景色为白色的二维码
img = qr.make_image(fill_color="red", back_color="white")

Here Insert Picture Description
3. In the two-dimensional code added to a picture
can take advantage of PIL image library module paste function

# img为二维码图片对象,path标识要添加的图片对象,where表示图片位置
img.paste(path,where,mask=None)

Wherein, img of the object image; path of the added image; WHERE is a tuple, such as: (x, y), where the images represent horizontal and vertical coordinates of the two-dimensional code.
To achieve more complex, see the real part of the source code, thank you!

Third, actual

# -*- coding: utf-8 -*-
# 生成二维码
import qrcode
from PIL import Image


# 生成二维码
def mark_qrcode(data):
    img = qrcode.make(data)
    img.show()
    img.save("qr_code.png")


def mark_my_qrcode(data):
    qr = qrcode.QRCode(
        version=4,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        border=4,
    )
    qr.add_data(data)
    qr.make(fit=True)
    # 设置红色白底的二维码
    img = qr.make_image(fill_color="red", back_color="white")
    img_w, img_h = img.size
    # 获取logo
    icon = Image.open("m1090760001.jpg")
    icon_w, icon_h = icon.size

    # 设置图片最大尺寸6:1
    factor = 6
    size_w = int(img_w / factor)
    size_h = int(img_h / factor)
    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)
    # 获取中心的坐标
    c_w = int((img_w - icon_w) / 2)
    c_h = int((img_h - icon_h) / 2)
    img.paste(icon, (c_w, c_h), mask=None)
    img.show()


if __name__ == "__main__":
    data = "https://blog.csdn.net/m1090760001"
    # mark_qrcode()
    mark_my_qrcode(data)

Picture the actual effect generated as follows (run the above code readers, please prepare a picture, or will be error):
Here Insert Picture Description

Published 19 original articles · won praise 67 · views 20000 +

Guess you like

Origin blog.csdn.net/m1090760001/article/details/103192295