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

Picamera2每日拍照脚本报错:'CompletedRequest'无completed/wait属性

解决Picamera2每日拍照脚本的属性错误问题

问题根源

  • picam2.capture_request()返回的CompletedRequest对象本身就是已完成的捕获请求,不存在completed属性或wait()方法,原代码里的等待逻辑完全多余。
  • 原代码获取图像数据的方式不符合Picamera2的API规范,且未释放请求资源,可能引发内存占用问题。

修改后的完整脚本

#!/usr/bin/python3
import time
import datetime
import pytz
from picamera2 import Picamera2

# 设置英国时区
tz = pytz.timezone('Europe/London')

picam2 = Picamera2()
picam2.configure("still")
picam2.start()

# 等待自动曝光和自动白平衡稳定后禁用
time.sleep(1)
picam2.set_controls({"AeEnable": False, "AwbEnable": False, "FrameRate": 1.0})
time.sleep(1)

while True:
    # 获取当前英国时间
    now = datetime.datetime.now(tz)

    # 判断是否在白天(8:00-18:00)
    if 8 <= now.hour < 18:
        # 捕获图像请求
        r = picam2.capture_request()
        # 直接生成PIL图像对象
        image = r.make_image()
        
        # 按日期+年内天数命名保存
        filename = now.strftime("%Y-%m-%d_Day%j.jpg")
        image.save(filename)
        
        print(f"已拍摄并保存: {filename} | 时间: {now.strftime('%Y-%m-%d %H:%M:%S')}")
        
        # 必须释放请求资源
        r.release()
        
        # 等待到次日
        next_day = now.replace(hour=0, minute=0, second=0, microsecond=0) + datetime.timedelta(days=1)
        sleep_time = (next_day - now).total_seconds()
        time.sleep(sleep_time)
    else:
        # 等待到下一个白天
        next_daytime = now.replace(hour=8, minute=0, second=0, microsecond=0)
        if now.hour >= 18:
            next_daytime += datetime.timedelta(days=1)
        sleep_time = (next_daytime - now).total_seconds()
        time.sleep(sleep_time)

picam2.stop()

关键修改说明

  1. 移除了while not r.completed和r.wait()的无效代码,因为capture_request()返回时请求已经完成
  2. 用r.make_image()替代Image.frombytes,这是Picamera2官方推荐的获取PIL图像的方式
  3. 添加r.release()释放请求资源,避免长时间运行后内存溢出
  4. 保留了原有的时区设置、白天判断和定时逻辑,确保功能符合需求

内容的提问来源于stack exchange,提问作者jeffisnoob

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 17:07:53