图像的web框架上的编码和解码

编码发送

python 复制代码
#!/usr/bin/env python

import requests, json
import base64

ip = "127.0.0.1"


headers = {'content-type': 'application/json'}

header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',
               "Content-Type": "application/json"}
url = 'http://{}:4488/'.format(ip)

if __name__ == "__main__":
    with open('resources/00.png', 'rb') as image_file:
        encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
    post_data = {"is_url": 0,
                 "data": encoded_image}
    post_data = json.dumps(post_data).encode("utf-8")
    resp = requests.post(url=url, headers=header_dict, data=post_data)
    print(resp.json())
    # print(resp.text) 如果返回的是text
    print(resp.status_code)

解码读取

python 复制代码
if req_data["is_url"]:
    image_url = req_data["data"]
    if not tools.download_file(req_data["data"], image_save_path):
        _info = f"[{image_url}] download failure, please check your url"
        self.logger.info(_info)
        return {"message": _info, "label": None}, 403
else:
    img_data = base64.b64decode(req_data["data"])
    bs = np.asarray(bytearray(img_data), dtype='uint8')
    img = cv2.imdecode(bs, cv2.IMREAD_COLOR)
    cv2.imwrite(image_save_path, img)
    self.logger.info(f"get image success...")
相关推荐
DN202015 分钟前
AI销售:从不迟到早退,永远秒回,您的忠实员工
人工智能·python
编程之升级打怪26 分钟前
Python的图形框架tkinter使用案例
python
liu****1 小时前
3.RNN及其变体
人工智能·python·rnn·深度学习
大江东去浪淘尽千古风流人物2 小时前
【Sensor】IMU传感器选型车轨级 VS 消费级
人工智能·python·算法·机器学习·机器人
码农小韩3 小时前
AIAgent应用开发——DeepSeek分析(一)
人工智能·python·深度学习·agent·强化学习
学Linux的语莫3 小时前
skills的使用
java·数据库·python
像风一样的男人@4 小时前
python --打包pyd或so文件
开发语言·python
MediaTea5 小时前
Python:生成器对象的扩展接口
开发语言·网络·python
Katecat996635 小时前
基于FCOS-HRNetV2P的高空作业安全装备检测与违规行为识别系统
python
好家伙VCC5 小时前
# 发散创新:基于Python的轻量级测试框架设计与实践 在现代软件开发中,**自动化
java·开发语言·python·自动化