Skip to content

Instantly share code, notes, and snippets.

@wwj718
Last active January 16, 2024 13:21
Show Gist options
  • Save wwj718/95add9a9306cbdbe01ed02a5a869e15d to your computer and use it in GitHub Desktop.
Save wwj718/95add9a9306cbdbe01ed02a5a869e15d to your computer and use it in GitHub Desktop.
# pip install paho-mqtt
import time
import base64
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.username_pw_set('guest', 'test')
client.connect("mqtt.aimaker.space", 1883, 60)
client.loop_start() # 非阻塞
time.sleep(1)
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
def send_to_snap(image_path):
'''
png image
image_path = "/tmp/test.png"
'''
topic = "costume/cat"
payload = _image_to_base64_url(image_path)
client.publish(topic, payload)
send_to_snap("/tmp/test.png")
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment