前言

python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import base64

def image_to_base64_url(image_path):
    with open(image_path, "rb") as image_file:
        # 读取图像文件的二进制数据
        image_binary = image_file.read()

        # 将二进制数据编码为 Base64 字符串
        base64_string = base64.b64encode(image_binary).decode('utf-8')

        # 构建 URL 格式的 Base64 字符串
        base64_url = f"data:image/png;base64,{base64_string}"

        return base64_url

# 用法示例
image_path = "/tmp/test.png"
base64_url_result = image_to_base64_url(image_path)

print("Base64 URL:")
print(base64_url_result)

MQTT 发送不同 png 图片

参考