图像的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...")
相关推荐
YoungHong199211 小时前
【Python进阶】告别繁琐Debug!Loguru一键输出异常日志与变量值
python·debug·异常处理·日志·loguru·log·logger
AiXed12 小时前
PC微信协议之nid算法
python·网络协议·算法·微信
小李哥哥13 小时前
基于数据的人工智能建模流程及源码示例
python
APIshop13 小时前
实战解析:苏宁易购 item_search 按关键字搜索商品API接口
开发语言·chrome·python
蓝桉~MLGT13 小时前
Python学习历程——Python面向对象编程详解
开发语言·python·学习
larance13 小时前
Python 中的 *args 和 **kwargs
开发语言·python
百锦再13 小时前
选择Rust的理由:从内存管理到抛弃抽象
android·java·开发语言·后端·python·rust·go
yaoxin52112313 小时前
238. Java 集合 - 使用 ListIterator 遍历 List 元素
java·python·list
nvd1113 小时前
python 后端流式处理 LLM 响应数据详解
开发语言·python
F_D_Z14 小时前
【解决办法】报错Found dtype Long but expected Float
人工智能·python