问题描述
我使用 img = imageio.imread('hello.jpg')
加载图像。
我想将这个 numpy 数组作为图像返回。我知道我可以做到return FileResponse('hello.jpg')
。但将来我会将图片作为 numpy 数组。
如何从 fastapi 以等效于 img
的方式返回 numpy 数组 return FileResponse('hello.jpg')
?
解决方法
例如,您可以使用 StreamingResponse (https://fastapi.tiangolo.com/advanced/custom-response/#using-streamingresponse-with-file-like-objects) 来执行此操作,但在此之前您需要将 numpy 数组转换为 io.BytesIO
或 io.StringIO