Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to generate your own characteristic QR code in Python

Shulou Source: shulou.com Published: 2022-06-02 08:19:14 10月04日 Update

In this issue, the editor will bring you about how to generate your own characteristic QR code in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The qrcode library uses def gen_qrcode (text):

# create qrcode object

Qr = qrcode.QRCode (

Version=2

Error_correction=qrcode.constants.ERROR_CORRECT_Q

Box_size=10

Border=2

)

Qr.add_data (text)

# create a QR code picture

Img = qr.make_image ()

# convert the picture to RGBA format

Img = img.convert ('RGBA')

# return the size of the QR code image

Img_w, img_h = img.size

# Open logo

Logo = Image.open (os.path.join (BASE_DIR, 'static/images/logo.jpeg'))

# the logo size is 1/4 of the QR code

Logo_w = img_w / / 4

Logo_h = img_w / / 4

# modify logo image size

Logo = logo.resize ((logo_w, logo_h), Image.ANTIALIAS)

# put logo in the middle of the QR code

W = (img_w-logo_w) / / 2

H = (img_h-logo_h) / / 2

Img.paste (logo (w, h))

Qr_path = 'static/imgcard/%s.png'% time.time ()

Img.save (os.path.join (BASE_DIR, qr_path))

Return qr_path

Parameter meaning:

Version: an integer with a value of 1 to 40 that controls the size of the QR code (the minimum value is 1, which is a 12 × 12 matrix)

Error_correction: controls the error correction function of QR code. You can take the following four constants.

ERROR_CORRECT_L: about 7% or less of errors can be corrected.

ERROR_CORRECT_M (default): about 15% or less of errors can be corrected.

ROR_CORRECT_H: about 30% or less of errors can be corrected.

Box_size: controls the number of pixels contained in each grid in the QR code.

Border: controls the number of squares contained in the border (the distance between the QR code and the image boundary) (the default is 4, which is the minimum specified by the relevant standards)

The myqr library uses from MyQR import myqr

Myqr.run (

Fill in the link or text content in words=' http://www.baidu.com', # words

Version=1, # controls the QR code size from 1 to 40. It is recommended that you do not exceed 5, otherwise the generation time is too long.

Level='H', # controls the error correction level, which ranges from L, M, Q, H, and rises sequentially from left to right.

Picture='background.gif', # background image, which can be in .jpg, .png, .bmp, .gif format

Colorized=True, # can change the resulting picture from black and white (False) to color (True)

Contrast=1.0, # is used to adjust the contrast of the picture, 1. 0 represents the original picture, a smaller value indicates lower contrast, and vice versa. The default is 1.0.

Brightness=1.0, # is used to adjust the brightness of the picture

)

The above is the editor for you to share how to generate their own characteristic QR code in Python, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: 2D QR code picture control size error generation content feature minimum contrast grid format analysis adjustment original medium and small brightness pixels number of pixels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Docker Apple OPPO Reno