python uses werobot to process pictures in the development of wechat official account

python uses werobot to process pictures in the development of wechat official account

preface

Xiaobai, I am interested in studying the development of python wechat official account recently. There are many tutorials about server configuration, which will not be released here for the time being (I planned to do intranet penetration myself, but I didn't do it in the end, so I rented Alibaba cloud's server). In addition, there are many codes on the Internet to realize the "repeater" function, that is, when you send any text message or picture message, the official account will reply to the message exactly the same as that you sent. In fact, it does not process the sent message. As for the part of image processing introduced in this article, the wechat open document introduces too much theory, does not give specific code, and [werobot]( https://werobot.readthedocs.io/zh_CN/latest/ )The document only gives the usage of the function, and it is not very detailed. There is no specific implementation method found on Baidu. So I write this blog to record the process of my own research

1, werobot implementation of text repeater

The code for werobot to implement the repeater is given in the werobot document (the link is given above). I post it mainly to compare the later processing parts.

import werobot

robot = werobot.WeRoBot(token='You set it yourself on the wechat public platform token')

# @robot. The text decorated Handler handles only text messages
@robot.text
def echo(message):
    return message.content

# Let the server listen at 0.0.0.0:80
robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()

2, Image processing

In the wechat open document, there is a description of the image message xml, of which the most important are PicUrl and mediaid. The only way to get the picture from picaid is to send the picture to the picaid server. If there is an address in the picaid server, we must send the picture to the picaid server.

Now, if you want to do nothing and send the image received by the server back to the user intact, it's good to send the received mediaid directly.

from werobot.replies import ImageReply

@robot.image
def img(message):
    reply = ImageReply(message=message,media_id=message.MediaId)
    return reply

Now, the problem arises. If the image is processed, the mediaid will change. How can the mediaid of the processed image be obtained? At this time, new temporary materials will be used

You can see that the input of the function is the media type (picture type) and the file to be uploaded (processed picture), and there is mediaid in the returned json package. There is a very important problem here. I searched for a long time to solve it, that is, the file to be uploaded must be of file object type! Finally, send the obtained mediaid to the user. And the operation of relevant materials requires access_token, but configure your app in the werobot module_ ID and app_ Just secret, access_ It seems that the token will be obtained automatically.

Full code:

import werobot
import requests
import cv2
import numpy as np
from io import BytesIO,BufferedReader
from PIL import Image
from werobot.replies import ImageReply

robot = werobot.WeRoBot(token='Yours token')
robot.config["APP_ID"]="Yours APP_ID"
robot.config["APP_SECRET"]="Yours APP_SECRET"
client=robot.client

def ps(message):
	#part1
    req = requests.get(message.img)
    bytes_stream = BytesIO(req.content)
    img = Image.open(bytes_stream)     #PIL picture

	#part2
    img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)     #Converted gray image
    inv = 255 - gray          #Reverse
    ksize = 15
    sigma = 50
    blur = cv2.GaussianBlur(inv,ksize =(ksize,ksize),sigmaX = sigma,sigmaY = sigma)  #Gaussian filtering
    res = cv2.divide(gray,255-blur,scale = 255)

	#part3
    ret, img_encode = cv2.imencode('.jpg', res)
    str_encode = img_encode.tobytes()  # Convert array to binary type
    f4 = BytesIO(str_encode)  # Translate into_ io.BytesIO type
    f4.name = 'asdasdasd.jpg'  #Give him a name. If you don't name him, you will report an error
    f5 = BufferedReader(f4)  # Translate into_ io.BufferedReader class

	#part4
    return_json = client.upload_media("image",f5)
    mediaid = return_json["media_id"]
    return mediaid

@robot.image
def img(message):
    mediaid = ps(message)
    reply = ImageReply(message=message,media_id=mediaid)
    return reply

# Let the server listen at 0.0.0.0:80
robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()

The intermediate ps function consists of four parts.
part1 is to download the url and open it with pilot and load it into memory
part2 is a simple processing of color image to sketch I refer to. Because it is implemented with opencv, it is easy to convert cv image processing
part3 is to convert cv image into file object type for Material upload
part4 uploads the material and obtains the mediaid of the new material

The test effect is shown in the figure

Tags: Python

Posted by why not on Mon, 02 May 2022 15:42:06 +0300