Python 将numpy数组作为图像从fastapi返回

Python 将numpy数组作为图像从fastapi返回,python,numpy,fastapi,python-imageio,Python,Numpy,Fastapi,Python Imageio,我加载一个带有img=imageio.imread('hello.jpg')的图像。 我想返回这个numpy数组作为图像。我知道我可以返回FileResponse('hello.jpg')。但在未来,我将把这些图片作为numpy阵列 如何从fastapi返回numpy数组img,使其与返回FileResponse('hello.jpg')等效?您可以使用StreamingResponse()来执行此操作,例如,但在您需要将numpy数组转换为io.BytesIO或io.StringIO

我加载一个带有
img=imageio.imread('hello.jpg')
的图像。 我想返回这个numpy数组作为图像。我知道我可以
返回FileResponse('hello.jpg')
。但在未来,我将把这些图片作为numpy阵列


如何从fastapi返回numpy数组
img
,使其与返回FileResponse('hello.jpg')等效?

您可以使用StreamingResponse()来执行此操作,例如,但在您需要将numpy数组转换为
io.BytesIO
io.StringIO