Use WeChat QR codes to achieve a happy Mid-Autumn Festival

Environment preparation:

1. python environment;

2. Apply for WeChat public account;

The idea is to hide the Happy Mid-Autumn Festival page link of the WeChat official account in the QR code. If the person you send scans the QR code, the corresponding Mid-Autumn Festival blessing page will pop up. (*^▽^*)

First, let’s explain how to register a WeChat official account?

Register a WeChat public account and use the link below to register a WeChat public test account

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

First, you need to install the library, which you can do using pip:

pip install qrcode

Then, you can use the following code to generate a QR code:

import qrcode  
  
# 创建 qr 对象  
qr = qrcode.QRCode(  
    version=1,  
    error_correction=qrcode.constants.ERROR_CORRECT_H,  
    box_size=10,  
    border=4,  
)  
  
# 添加数据到 qr 对象  
qr.add_data('https://www.example.com')  
qr.make(fit=True)  
  
# 创建二维码图片  
img = qr.make_image(fill_color="black", back_color="white")  
  
# 保存二维码图片  
img.save('qrcode_example.png')

In this example, we generated a https://www.example.comQR code containing. You can replace this URL with the content you want (such as the link page to the WeChat official account for the Mid-Autumn Festival we mentioned above). The generated QR code will be saved as qrcode_example.png.

Note: versionThe parameter is an integer, indicating the size of the QR code. A QR code with version 1 is a 21x21 matrix. Every time the version increases by 1, the size of the matrix increases by 4, so a QR code with version 2 is a 25x25 matrix, and so on. box_sizeRepresents the pixel size of each point and borderrepresents the width of the border (referring to the number of points).

If you want to generate a dynamic QR code, you can do it as follows:

  • Use the MyQR module to generate dynamic QR codes. The MyQR module is a Python library for generating personalized QR codes. It can encode any text or URL into a QR code image, and add pictures, animations, colors and other effects to it. To use the MyQR module you need to install it first, you can use the pip command
pip install myqr

You can then use the following code to generate a file containing https://www.bing.com

dynamic QR code, add an animated image as the background, and save it as code.gif

from MyQR import myqr
myqr.run(
    words='https://www.bing.com', # 二维码包含的信息
    picture='picture.gif', # 背景图片的路径
    colorized=True, # 是否有颜色,如果为False则为黑白
    save_name='code.gif' # 输出文件名
)

Or write the following part of the code:

from myqr import MyQR  
import matplotlib.pyplot as plt  
from PIL import Image  
  
# 你可以替换成你想要的链接  
link = "https://example.com"  
  
# 你可以调整亮度,范围是0-1  
brightness = 1.0  
  
# 你需要指定一个.gif文件作为背景,生成的二维码会以这个为背景显示动态效果  
myqr = MyQR(link, '1.gif', brightness=brightness)  
myqr.run()

This code will generate a QR code with the GIF file you specify as the background. The linked text will be displayed on the GIF dynamic image after the QR code is generated. Please note that this code will only work if your QR code generator supports GIF images as backgrounds.

In addition, this method may be subject to some limitations. For example, some plug-ins or libraries may not support adding GIF animations to QR codes. In this case, you may need to find other libraries or methods to achieve your needs.

Registering a personal WeChat public account is divided into two steps:

The first is to register on the official website of WeChat public platform.

The steps are as follows: Open the browser, enter the official website of WeChat public platform (https://mp.weixin.qq.com/), click on the upper right corner to register immediately, select the subscription number, fill in the email address and log in to receive the activation email, fill in the email verification code To activate, select the personal type in the information registration and fill in the ID card information, then fill in the official account name, function introduction and select the operating area, and finally click to register.

The second is to scan the QR code on WeChat to register an account.

Open WeChat, click "Scan" in the upper right corner, scan the QR code to enter the WeChat public platform registration page, click "Register Now" in the upper right corner, enter the personal operation or company operation page, fill in the corresponding information and click to register. .

Please note that the information filled in when registering the official account must be true and valid.

Use the WeChat public account registered above to publish a beautiful article, and then copy the link and replace the link in the above code to generate the target QR code. Then you can use this QR code to send it to good friends or relatives. Got it! ! !

Just select your subscription number to register.

Guess you like

Origin blog.csdn.net/Helloorld_1/article/details/132854576