PiCamera将流数据保存到图像文件中

时间:2018-06-28 23:23:10

标签: python io stream raspberry-pi

以下示例代码将图像保存到流中。我想知道如何将此流中的图像保存到Pi SD卡上的图像文件(.jpg等)中,最好是在捕获所有图像以保持较高的FPS之后。

import io
import time
import picamera

with picamera.PiCamera() as camera:
    # Set the camera's resolution to VGA @40fps and give it a couple
    # of seconds to measure exposure etc.
    camera.resolution = (640, 480)
    camera.framerate = 80
    time.sleep(2)
    # Set up 40 in-memory streams
    outputs = [io.BytesIO() for i in range(40)]
    start = time.time()
    camera.capture_sequence(outputs, 'jpeg', use_video_port=True)
    finish = time.time()
    # How fast were we?
    print('Captured 40 images at %.2ffps' % (40 / (finish - start)))

picamera文档: http://picamera.readthedocs.io/en/release-1.10/api_camera.html

1 个答案:

答案 0 :(得分:1)

使用PIL。在picam文档中也有一个示例。

import io
import time
import picamera

from PIL import Image

with picamera.PiCamera() as camera:
    # Set the camera's resolution to VGA @40fps and give it a couple
    # of seconds to measure exposure etc.
    camera.resolution = (1920, 1080)
    camera.framerate = 15
    camera.rotation = 180
    time.sleep(2)
    # Set up 40 in-memory streams
    outputs = [io.BytesIO() for i in range(40)]
    start = time.time()
    camera.capture_sequence(outputs, 'jpeg', use_video_port=True)

    finish = time.time()
    # How fast were we?
    print('Captured 40 images at %.2ffps' % (40 / (finish - start)))

    count = 0
    for frameData in outputs:
        rawIO = frameData
        rawIO.seek(0)
        byteImg = Image.open(rawIO)

        count += 1
        filename = "image" + str(count) + ".jpg"
        byteImg.save(filename, 'JPEG')