python - 如何保存通过camera.capture_continuous(格式rgb)读取的图像并将其保存到文件中

标签 python python-3.x camera raspberry-pi google-coral

我通过 camera.capture_continuous(stream,format='rgb', use_video_port=True, resize=(width, height) 读取 raspi 摄像头,将其馈送到 Coral Edge 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

我做错了什么?

最佳答案

用下面的行解决了它

image = Image.frombuffer('RGB', (width,height), streamValue)

关于python - 如何保存通过camera.capture_continuous(格式rgb)读取的图像并将其保存到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57664449/

相关文章:

Android 无法连接相机服务?

python - 我缺少使用 Django 和 Python 编辑数据库记录的内容

python - 根据用户输入的条件打印 .csv 中的行

ios - 检索 iOS 摄像机分辨率

image - 如何使用 MATLAB 以定时间隔采集图像?

python - 由于 gcc 错误,无法使用 pip 安装 sasl

python - 使用 Dateutil 的相对日期基准

python - 如何在 azure ml studio 的 'execute python script' 部分中将外部 pickle 文件内容作为数据帧加载?

Python Excel 图表类型 pywintypes.com_error

python - 将 df1 中的值替换为 df2 中的值,然后在新列中分配代码(如果值被替换)