图像的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...")
相关推荐
0566466 小时前
Python康复训练——常用标准库
开发语言·python·学习
0566466 小时前
Python康复训练——控制流与函数
开发语言·python·学习
天使day7 小时前
FastAPI快速入门
python·fastapi
databook7 小时前
用相关性分析消除“冗余特征”
python·机器学习·scikit-learn
幻想时空8 小时前
地图数据采集
python
梦想不只是梦与想8 小时前
Python 中的类型判断方法
python·type·isinstance
alphaTao8 小时前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
Kevin Wang7279 小时前
Nvidia-AGX-spark部署手册——课堂质量诊断(jetpack:r36)
python·docker·容器
Python私教9 小时前
Django 6.1 RC1 实测:FETCH_PEERS 两条 SQL 解决 N+1,select_related 还需要吗?
后端·python·django
玉鸯10 小时前
Agent 的任务编排:从 System Prompt 到 Hierarchical Multi-Agent
python·llm·agent