图像的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...")
相关推荐
m0_702036532 分钟前
CSS如何兼容新旧方案结合响应式容器查询
jvm·数据库·python
ZHW_AI课题组13 分钟前
基于ElasticNet网格搜索的汽车燃油效率预测
python·机器学习·回归算法
在坚持一下我可没意见22 分钟前
Python 修仙修炼录 05:循环神通,省去无用苦修
开发语言·python·面试·入门·循环·复习
大飞记Python43 分钟前
从“驱动地狱”到一行代码:WebDriverManager使用手记(附模板)
python·测试
Cloud_Shy6181 小时前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(第九章 Excel 自动化 上篇)
python·数据分析·excel·numpy·pandas
子午1 小时前
基于YOLO的玫瑰叶片检测系统~Python+深度学习+人工智能+目标检测+YOLOV8算法
人工智能·python·yolo
Hesionberger1 小时前
LeetCode 101:对称二叉树(多语言解法)
开发语言·python
小陈的进阶之路1 小时前
Python系列课(11)——PySpark
开发语言·python·ajax
WL_Aurora1 小时前
备战蓝桥杯国赛【Day 12】
python·蓝桥杯
Cloud_Shy6181 小时前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(第八章 使用读写包操作 Excel 文件 下篇)
python·数据分析·excel·numpy·pandas