图像的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...")
相关推荐
xixihaha13244 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
xixihaha13244 小时前
Python游戏中的碰撞检测实现
jvm·数据库·python
ID_180079054735 小时前
模拟1688商品详情的Python API实现,返回符合风格的JSON数据
开发语言·python·json
程序员小远5 小时前
软件测试之功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
我的xiaodoujiao6 小时前
API接口自动化测试详细图文教程学习系列1--序章
python·学习·pytest
ZhengEnCi6 小时前
P1B-Python环境配置基础完全指南-Windows系统安装与验证
python
m0_716667076 小时前
NumPy入门:高性能科学计算的基础
jvm·数据库·python
带娃的IT创业者6 小时前
Weclaw 请求路由实战:一个 request_id 如何在 800 个并发连接中精准找到目标浏览器?
python·websocket·fastapi·架构设计·实时通信·openclaw·weclaw
2401_844221327 小时前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
白雨青8 小时前
国信 iQuant 自动国债逆回购实战:Python 自动化闲钱理财
python·量化策略·量化交易·国债逆回购