树莓派;使用 geany(.py) 创建一个新文件夹并为其添加地址代码?

问题描述 投票:0回答:1

抱歉,如果这是显而易见的;总的来说,我对 Rpi 和 Linux 很陌生。 我正在尝试为延时相机创建代码,以便当它打开时,它开始拍摄图像并将它们保存到文件夹中。

我找不到任何信息(或者如果可能的话)是如何在每次代码开始运行时生成一个新的(唯一的)子文件夹,然后将新照片发送到该位置?

实际拍照代码超级简单:

for i in range(numphotos):
    camera.capture('/home/pi/Pictures/image{0:06d}.jpg'.format(i))
    sleep(secondsinterval)

我当前的解决方案是将它们全部转储到具有唯一日期和时间戳的同一个文件夹中,这样如果必须重新启动,它就有一个明确的开始和结束:

dateraw= datetime.datetime.now()
datetimeformat = dateraw.strftime("%Y-%m-%d_%H:%M")

for i in range(numphotos):
    camera.capture('/home/pi/Pictures/'+ datetimeformat + '_{0:06d}.jpg'.format(i))
    sleep(secondsinterval)

但正如我所说,有什么方法可以编写代码,而不是将其定向到图片文件夹,以便它生成一个唯一的文件夹(可能基于日期)并将图像发送到那里?

python raspberry-pi self-reference geany timelapse
1个回答
0
投票

事实证明这非常简单。

最终代码:

from picamera import PiCamera
import os
from os import system
import datetime
from time import sleep

timeMinutes = 2 #minutes you wish to run your timelapse camera
secondsInterval = 1 #seconds of delay between each photo

numPhotos = int((timeMinutes*60)/secondsInterval) #number of photos to take
print("number of photos to take = ", numPhotos)

dateRaw= datetime.datetime.now()
startDateTimeFormat = dateRaw.strftime("%Y-%m-%d_%H:%M:%S")

os.mkdir('/home/timelapse1/Pictures/' + startDateTimeFormat)
print("The Pi has now started taking photos for your timelapse, start time: " + startDateTimeFormat)

camera = PiCamera()
camera.resolution = (4000, 3000)

for i in range(numPhotos):
    camera.capture('/home/timelapse1/Pictures/' + startDateTimeFormat +'/{0:06d}.jpg'.format(i))
    sleep(secondsInterval)

dateRaw= datetime.datetime.now()
endDateTimeFormat = dateRaw.strftime("%Y-%m-%d_%H:%M")
print("Photos finished on " + endDateTimeFormat + ". Files located at /home/timelapse1/Pictures/" + startDateTimeFormat)
© www.soinside.com 2019 - 2024. All rights reserved.