python - 从 fastapi 返回 numpy 数组作为图像
问题描述
我用img = imageio.imread('hello.jpg')
. 我想将此 numpy 数组作为图像返回。我知道我能做到return FileResponse('hello.jpg')
。但在未来我会将图片作为 numpy 数组。
如何以img
等效于的方式从 fastapi返回 numpy 数组return FileResponse('hello.jpg')
?
解决方案
您可以使用 StreamingResponse ( https://fastapi.tiangolo.com/advanced/custom-response/#using-streamingresponse-with-file-like-objects ) 来执行此操作,例如,但在您需要将 numpy 数组转换为io.BytesIO
或_io.StringIO
推荐阅读
- objective-c - 将 CIAffineClamp 和 CIGaussianBlur 与 CALayer backgroundFilters 一起使用
- java - 从 Java 中的 DataFrame 中查找每天的最大行程数 - Spark
- jupyter-notebook - Jupyter Notebook 下载为 LaTeX 或 PDF 失败,但使用 nbconvert 从命令行工作
- javascript - jQuery 选择器问题 - 尝试选择和记录链接
- python - 用户选择/键入所需页面的 Django 分页
- ms-word - 动态显示文本 MS Word 邮件合并
- django - 创建配置文件编辑视图
- sprite-kit - UIPanGestureRecognizer 未按预期运行
- pandas - 有效地使用混合类型更改数据帧值 dtype
- firebase - 用于在用户对用户聊天中存储数千条消息的 Firestore?