gpt4 book ai didi

python - "Object not callable"回显发布的图像时

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:36 24 4
gpt4 key购买 nike

我正在尝试监听使用 'image' 键通过 post 发送的文件,然后将其返回给调用者。

from flask import Flask, request, make_response
app = Flask(__name__)

@app.route('/', methods = ['POST', 'GET'])
def img():
if request.method == 'GET':
return "yo, you forgot to post"
else:
img = request.files['image']
resp = make_response(img)
resp.status_code = 200
return resp

if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0')

此操作失败并显示消息:

TypeError: 'FileStorage' object is not callable

我对 Python 不太熟悉,但我认为通过执行上述操作,我是直接通过内存传递 byte[]

如何在不失败的情况下实现这一目标?我想从请求中获取图像 blob,对其进行操作(当前不是),然后直接将其返回。在内存中,而不保存到文件系统。

最佳答案

您可以使用 Flask.send_image() function 再次提供图像。 ;它接受类似文件的对象以及文件名。您必须将文件数据复制到内存中的文件对象(request.files 文件对象关闭得太早,不适合):

from io import BytesIO

return send_file(BytesIO(request.files['image'].read())

您可能需要向响应添加其他 header (例如 mime 类型),因为 send_file() 函数在此提供的信息少于“常规”文件对象。发送浏览器可能包含内容类型,例如:

import mimetypes

img = request.files['image']
mimetype = img.mimetype or mimetypes.guess_type(img.filename)[0]
return send_file(BytesIO(img.read()), mimetype=mimetype)

这确实需要您将整个图像保存在内存中。

关于python - "Object not callable"回显发布的图像时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com