图像的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...")
相关推荐
程序员杰哥23 分钟前
性能测试详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
人工智能AI技术1 小时前
【Agent从入门到实践】42实战:用Docker打包Agent,实现一键部署
人工智能·python
开发者小天1 小时前
python中的class类
开发语言·python
idwangzhen1 小时前
GEO优化系统哪家更专业
python·信息可视化
diediedei2 小时前
机器学习模型部署:将模型转化为Web API
jvm·数据库·python
m0_561359672 小时前
使用Python自动收发邮件
jvm·数据库·python
naruto_lnq2 小时前
用Python批量处理Excel和CSV文件
jvm·数据库·python
b2077212 小时前
Flutter for OpenHarmony 身体健康状况记录App实战 - 提醒设置实现
python·flutter·macos·cocoa·harmonyos
2301_822365032 小时前
数据分析与科学计算
jvm·数据库·python
河北小博博2 小时前
分布式系统稳定性基石:熔断与限流的深度解析(附Python实战)
java·开发语言·python