首页 > 解决方案 > 从 fastapi 返回 numpy 数组作为图像

问题描述

我用img = imageio.imread('hello.jpg'). 我想将此 numpy 数组作为图像返回。我知道我能做到return FileResponse('hello.jpg')。但在未来我会将图片作为 numpy 数组。

如何以img等效于的方式从 fastapi返回 numpy 数组return FileResponse('hello.jpg')

标签: pythonnumpyfastapipython-imageio

解决方案


您可以使用 StreamingResponse ( https://fastapi.tiangolo.com/advanced/custom-response/#using-streamingresponse-with-file-like-objects ) 来执行此操作,例如,但在您需要将 numpy 数组转换为io.BytesIO或_io.StringIO


推荐阅读