很早的代码,今天清理下电脑,都要删掉了,发在博客上,希望能帮到有需要的人。
代码分为上传图片和获取图片数据流两部分,官方的文档在这
上传文件
获取文件
# -*- coding: utf-8 -*-
"""
@Time : 2019/3/21 8:38
@Author : later
@Email : [email protected]
@File : onenetPic.py
@Software: PyCharm
"""
import requests
#PIL图像处理标准库
from PIL import Image
from io import BytesIO
url = "http://api.heclouds.com/bindata"
url2 = "http://api.heclouds.com/bindata/60957xxx2_159556xxxx"
headers = {
"Content-Type": "charset=utf8",
"api-key": "3yOvwUx07iuxxxxxxxxtHYbnFkw="
}
headers2 = {
"api-key": "3yOvwUx07xxxxxxxxxxtHYbnFkw="
}
queryString = {"device_id": "60xxx392", "datastream_id": "PSM"}
with open('BackGround.jpg', 'rb') as f:
requests.post(url, params=queryString, headers=headers, data=f)
# 读取onenet二进制图片数据
res = requests.get(url2, headers=headers2).content
print(res)
# 去除换行符
hexData = res.hex()
hexData.replace('\r\n', '')
# 将16进制hexStr转回字节数组
pic = bytes.fromhex(hexData)
# 将bytes结果转化为字节流
bytes_stream = BytesIO(pic)
# 读取到图片
jpg = Image.open(bytes_stream)
# 展示图片
jpg.show()