python - 如何保存通过camera.capture_continuous(格式rgb)读取的图像并将其保存到文件中
问题描述
我通过 camera.capture_continuous(stream,format='rgb', use_video_port=True, resize=(width, height) 读取 raspi 相机,将其馈送到珊瑚边缘 USB 加速器。这非常有效。但现在我想保存某些图像(取决于分析)到硬盘。
我是 python 初学者... file.write 没有用。我假设是因为我得到了某种原始 rgb 图像数据而不是 jpg。
我希望能够将图像存储为 jpg。谁能建议使用什么功能?
更新
我尝试了以下
import argparse
import os
import io
import time
from collections import deque
import numpy as np
import picamera
from PIL import Image
import edgetpu.classification.engine
def main():
stream = io.BytesIO()
engine = edgetpu.classification.engine.ClassificationEngine(args.model)
for foo in camera.capture_continuous(stream,
format='rgb',
use_video_port=True,
resize=(width, height)):
stream.truncate()
stream.seek(0)
input = np.frombuffer(stream.getvalue(), dtype=np.uint8)
results = engine.ClassifyWithInputTensor(input, top_k=3)
...
image = Image.fromarray(input.astype('uint8'), 'RGB')
image.save("imgs/image_" + str(i) + ".jpg")
但只得到一个错误:
Traceback (most recent call last):
File "mio.py", line 85, in <module>
main()
File "mio.py", line 75, in main
image = Image.fromarray(input.astype('uint8'), 'RGB')
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2529, in fromarray
size = shape[1], shape[0]
IndexError: tuple index out of range
我究竟做错了什么?
解决方案
您可以使用Pillow库将图像保存到磁盘。就像是:
pip install Pillow numpy
import numpy as np
from PIL import Image
pixels = np.array([[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 0]]])
image = Image.fromarray(pixels.astype('uint8'), 'RGB')
image.save('out.jpg')
推荐阅读
- google-analytics - 为什么 Google Analytics(分析)报告中的一个页面会显示多个页面标题?
- java - Apache Camel Rest - 如何将请求和响应从上一个端点发送到下一个端点
- javascript - Javascript getmouse
- json - 在swift 4中保存并加载复杂的结构(可编码)到本地文件 - Xcode
- python - 查找对数表示矩阵的特征值
- python - 使用什么模型进行销售预测?
- github - Docker 容器卷未安装在 GitHub Actions 中
- go - 如何仅调用具有注释或标签的特定函数
- c++ - c ++在一行中将变量值添加到字符串
- xml - XSLT 避免 XML 元素中的 xmlns=""