You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PiCamera2中是否存在PiCamera的camera.capture_continuous()函数替代方案?

PiCamera2中是否存在PiCamera的camera.capture_continuous()函数替代方案?

嘿,我刚好了解这个问题!Picamera2确实没有和Picamera里camera.capture_continuous()完全一一对应的函数,但我们可以用它的基础捕获方法轻松实现相同的效果,甚至还能更灵活。

你原来的代码是循环捕获JPEG流并转成RGB格式的PIL图像,在Picamera2里可以通过两种方式实现类似逻辑:

方式一:延续BytesIO+JPEG的捕获方式

这种方式和你原来的代码逻辑最接近,直接把图像捕获到内存流中:

from picamera2 import Picamera2
import io
from PIL import Image

# 初始化并配置相机
picam2 = Picamera2()
# 创建JPEG格式的静态捕获配置
config = picam2.create_still_configuration(main={"format": "JPEG"})
picam2.configure(config)
picam2.start()

stream = io.BytesIO()
try:
    while True:
        # 每次捕获前重置流的位置和大小
        stream.seek(0)
        stream.truncate()
        # 捕获图像到内存流
        picam2.capture_file(stream, format="jpeg")
        # 重置流指针到开头,准备读取
        stream.seek(0)
        # 转成RGB格式的PIL图像
        image = Image.open(stream).convert('RGB')
        
        # 在这里添加你对image的处理逻辑,比如图像识别、保存等
finally:
    # 记得停止相机
    picam2.stop()

方式二:直接捕获RGB数组(更高效)

如果不需要JPEG编解码的环节,可以直接捕获RGB格式的数组,跳过流的操作,效率会更高:

from picamera2 import Picamera2
from PIL import Image

picam2 = Picamera2()
# 创建RGB888格式的静态捕获配置
config = picam2.create_still_configuration(main={"format": "RGB888"})
picam2.configure(config)
picam2.start()

try:
    while True:
        # 直接捕获RGB数组
        rgb_array = picam2.capture_array()
        # 转成PIL图像
        image = Image.fromarray(rgb_array)
        
        # 在这里添加你的处理逻辑
finally:
    picam2.stop()

简单来说,Picamera2把连续捕获的逻辑交给开发者自己通过循环实现,这样反而能根据需求灵活调整捕获的频率、格式等参数,比原来的capture_continuous更灵活哦。

备注:内容来源于stack exchange,提问作者Ara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 12:10:29