图像的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...")
相关推荐
键盘上的猫头鹰13 分钟前
【Linux 基础教程(四)】文件内容查看、打包压缩与搜索、重定向管道及环境变量
linux·服务器·python
独挽离人16 分钟前
【无标题】
python
天天进步20151 小时前
Python全栈项目--社区问答平台
开发语言·python·django
噜噜噜阿鲁~1 小时前
python学习笔记 | 12.0、错误、调试和测试
笔记·python·学习
AI视觉网奇1 小时前
Bambu Studio 发现 xx个开放边
开发语言·人工智能·python
SilentSamsara2 小时前
缓存策略实战:Redis + Python 多级缓存设计与失效策略
开发语言·redis·python·缓存·性能优化
本地化文档2 小时前
psycopg3-docs-l10n
数据库·python·postgresql·github·gitcode·sphinx
JeJe同学2 小时前
LabelImg 标签字体大小修改教程
linux·人工智能·python
Irissgwe2 小时前
【无标题】
python·langchain·ai编程·langgraph
RSTJ_16252 小时前
PYTHON+AI LLM DAY SIXTY-SIX
服务器·开发语言·python