Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从flask restful API返回图像?_Python_Flask_Flask Restful - Fatal编程技术网

Python 如何从flask restful API返回图像?

Python 如何从flask restful API返回图像?,python,flask,flask-restful,Python,Flask,Flask Restful,我想返回处理后的图像。到目前为止,我能够将映像发送到服务器和进程。如何返回图像以便任何客户端都可以使用 class ImageProcessing(Resource): def __init__(self): parser = reqparse.RequestParser() parser.add_argument("image", type=werkzeug.datastructures.FileStorage, required

我想返回处理后的图像。到目前为止,我能够将映像发送到服务器和进程。如何返回图像以便任何客户端都可以使用

class ImageProcessing(Resource):
 
    def __init__(self):
        parser = reqparse.RequestParser()
        parser.add_argument("image", type=werkzeug.datastructures.FileStorage, required=True, location='files')
        self.req_parser = parser
        
    def post(self):
        image_file = self.req_parser.parse_args(strict=True).get("image", None)
        if image_file:
            image = image_file.read()
            nparr = np.fromstring(image, np.uint8)
            img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
            img = process_img(img) 
            shape=img.shape
            return "Image recieved: Image size {}X{}X{}".format(shape[0],shape[1],shape[2])
        else:
            return "Image sending failed"

卷曲url:

curl -X POST -F 'image=@data/test.jpg' http://127.0.0.1:5000/processImage

如何返回处理后的图像?

没关系,我解决了这个问题,首先将图像转换为base64,然后将其作为字符串返回

 rawBytes = io.BytesIO()
 img.save(rawBytes, "JPEG")
 rawBytes.seek(0)
 img_base64 = base64.b64encode(rawBytes.read())
 response = {
         "shape": shape,
         "image": img_base64.decode(),
         "message":"Image is BASE 64 encoded"        
      }
 return response,200
你可以查看我对类似问题的回答