Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to return image after processing it. So far, I am able to get image to the server and process. How do I return the image so that it can be used by any client?

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"

Curl url:

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

How to return the processed image?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
467 views
Welcome To Ask or Share your Answers For Others

1 Answer

Never mind, I resolved the issue by first converting the image to base64 and then returning it as string.

 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...