图像的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...")
相关推荐
像风一样自由20207 分钟前
算法模型部署后_python脚本API测试指南-记录3
python·算法·支持向量机
HORSE RUNNING WILD1 小时前
解决 PicGo 上传 GitHub图床及Marp中Github图片编译常见难题指南
css·python·github
ElenaYu2 小时前
mac安装cast
python·macos·cast
Dxy12393102162 小时前
python如何设置excel单元格边框样式
开发语言·python·excel
chaodaibing2 小时前
Python解析Excel入库如何做到行的拆分
开发语言·python·excel
dudly2 小时前
Python类的力量:第五篇:魔法方法与协议——让类拥有Python的“超能力”
开发语言·python
攻城狮7号3 小时前
Python爬虫第21节- 基础图形验证码识别实战
开发语言·爬虫·python·图形验证码识别
kpl_203 小时前
Python基础
开发语言·python
啥都鼓捣的小yao4 小时前
课程11. 计算机视觉、自编码器和生成对抗网络 (GAN)
人工智能·python·深度学习·神经网络·算法·生成对抗网络·计算机视觉