PiCamera-如何将'rgb'格式的np.arrray捕获保存到文件?

问题描述 投票:0回答:1
import time
import picamera
import picamera.array
import numpy as np

with picamera.PiCamera() as camera:
    camera.resolution = (100,100)
    time.sleep(2)
    image = np.empty((128,112, 3), dtype=np.uint8)
    camera.capture(image, 'rgb')
    image = image[:100, :100]

我的问题是-如何将“图像”另存为.png文件?我希望为机器学习项目捕获图像。

help(camera.capture)的结果如下:

picamera.camera.PiCamera实例的

capture(output,format = None,use_video_port = False,resize = None,splitter_port = 0,bayer = False,** options)方法从相机捕获图像,并将其存储在输出

If *output* is a string, it will be treated as a filename for a new
file which the image will be written to. If *output* is not a string,
but is an object with a ``write`` method, it is assumed to be a
file-like object and the image data is appended to it (the
implementation only assumes the object has a ``write`` method - no
other methods are required but ``flush`` will be called at the end of
capture if it is present). If *output* is not a string, and has no
``write`` method it is assumed to be a writeable object implementing
the buffer protocol. In this case, the image data will be written
directly to the underlying buffer (which must be large enough to accept
the image data).

在我看来,'图像'正在进入底层缓冲区(对此我一无所知)。如何将缓冲区捕获到文件中?我是否在以错误的方式思考?

非常感谢!

python image raspberry-pi rgb
1个回答
1
投票

您可以使用PIL或其他库将Numpy数组转换为PIL Image并保存它...

from PIL import Image

im = Image.fromarray(YourNumpyImage)
im.save(’result.png’)
© www.soinside.com 2019 - 2024. All rights reserved.