python3 企业微信机器人发送图片

import requests
import base64
import hashlib


def wx_image(image):
    
    with open(image, 'rb') as file:                     #转换图片成base64格式
        data = file.read()
        encodestr = base64.b64encode(data)
        image_data = str(encodestr, 'utf-8')
        
    with open(image, 'rb') as file:                   #图片的MD5值
        md = hashlib.md5()
        md.update(file.read())
        image_md5 = md.hexdigest()
        
    url = "https://"                                        #填上机器人Webhook地址 
    headers = {"Content-Type": "application/json"}
    data = {
        "msgtype": "image",
        "image": {
            "base64": image_data,
            "md5": image_md5
        }
    }
    result = requests.post(url, headers=headers, json=data)
    return result
    
if __name__ == '__main__' :
    wx_image(image)

测试效果:
在这里插入图片描述
在这里插入图片描述
生成图片用的是matplotlib


版权声明:本文为weixin_46175620原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。