简体   繁体   English

如何返回图像作为 Flask 中 GET 请求的响应的一部分?

[英]How to return image as part of response from a GET request in Flask?

I have a basic Python Flask API set up right not that takes an ID as a parameter for a GET request.我有一个基本的 Python Flask API 设置权,它没有将 ID 作为 GET 请求的参数。 Inside the get request logic, I am doing a get request on another resource to retrieve an image associated with the ID that was passed in:在获取请求逻辑中,我正在对另一个资源执行获取请求,以检索与传入的 ID 关联的图像:

image = requests.get(url = image_url)
image = Image.open(BytesIO(image.content))
print(image.format)

The image format that it returns in JPEG .它以JPEG返回的图像格式。

I want to add this as part of the response of my API.我想将此添加为我的 API 响应的一部分。 This is the definition of my GET endpoint:这是我的 GET 端点的定义:

@app.route('/getmsg/', methods=['GET']) 
def respond():

This is how I am trying to return the image that I got from the other resource:这就是我试图返回从其他资源获得的图像的方式:

return {"image": send_file(image, mimetype='image/jpeg'), "data": jsonify(return_list)}

I already tried following this stackoverflow question: How to return images in flask response?我已经尝试过这个stackoverflow问题: How to return images in flask response?

Which is how I got to where I am at now.这就是我现在所处的位置。 I make a get request through this endpoint: http://localhost:5000/getmsg/?id=7187167507933759112 .我通过此端点发出获取请求: http://localhost:5000/getmsg/?id=7187167507933759112

I have also tried to return just the image like so:我也试图像这样返回图像:

return send_file(image, mimetype='image/jpeg')

But it gives me the same error.但它给了我同样的错误。

Everything works except the returning of the image.除了图像的返回外,一切正常。

I expect to be able to see the image in the response of the get request, but right now it gives 500 internal server error.我希望能够在 get 请求的响应中看到图像,但现在它给出了 500 内部服务器错误。

This is the response that I get in the terminal:这是我在终端中得到的响应:

TypeError: <Response 693 bytes [200 OK]> is not JSON serializable

Any guidance on what I am doing wrong would be greatly appreciated.任何关于我做错了什么的指导将不胜感激。 Thanks.谢谢。

From the documentation of send_file() :send_file()的文档中:

Sends the contents of a file to the client.将文件的内容发送到客户端。 This will use the most efficient method available and configured.这将使用最有效的可用和配置方法。

It means that send_file itself is a response.这意味着 send_file 本身就是一个响应。 That is why you are getting this error: Response 693 bytes [200 OK]这就是您收到此错误的原因: Response 693 bytes [200 OK]

One possible solution to send an image would be to base64_encode the file.发送图像的一种可能解决方案是对文件进行 base64_encode。 Something like this:像这样的东西:

with open("yourfile.ext", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

And then send the response like:然后像这样发送响应:

return {"image": encoded_string, "data": jsonify(return_list)}

And decode the string on the other end like:并解码另一端的字符串,如:

base64.b64decode(coded_string)

This is also make the size of the response smaller, thus improving performance as well.这也使响应的大小更小,从而也提高了性能。

Hope this helps.希望这可以帮助。 Good luck.祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用单个get请求从python烧瓶中的REST服务器返回连续响应 - How to a get a continuous response from a REST server return in python flask with a single get request 如何从flask中的中间件(app.before_request())返回flask响应? - How to return flask response from the middleware(app.before_request()) in flask? 如何从 Python Flask API 返回图像流和文本作为 JSON 响应 - How to return image stream and text as JSON response from Python Flask API 如何从烧瓶请求中本地保存图像? - How to save an image locally from flask request? 如何从 flask 宁静的 API 返回图像? - How to return image from flask restful API? 如何从反应上传图像并单击提交可以从 flask 后端获得响应 - How can I upload an image from react and on clicking submit can get the response from flask backend 如何从从 javascript 发送到 flask 的发布请求中获取返回值 - How can I get a return value from a post request sent from javascript to flask 如何从从 javascript 发送到 flask 的 post 请求中获取返回值 - How can I get a return value from a post request sent from javascript to flask 如何合并来自外部API的请求并在Flask中将其作为响应发送 - How to combine request from external API and send it as a response in Flask 如何从 url 中获取 zip 文件,然后返回 zip 文件作为 Z319C3206A7F10C1457C3BZ9116D 中的响应 - how to zip files from url and then return zip file as response in flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM