1+ # SPDX-FileCopyrightText: 2024 Melissa leBlanc-Williams for Adafruit Industries
2+ #
3+ # SPDX-License-Identifier: MIT
4+
15#!/usr/bin/env python3
26
37import os
1519# For intervals <2 sec, better just to use raspistill's timelapse feature.
1620
1721# Configurable stuff...
18- INTERVAL = 15 # Time between captures, in seconds
19- WIDTH = 1280 # Image width in pixels
20- HEIGHT = 720 # Image height in pixels
21- QUALITY = 51 # JPEG image quality (0-100)
22- DEST = "/home/pi/timelapse" # Destination directory (MUST NOT CONTAIN NUMBERS)
23- PREFIX = "img" # Image prefix (MUST NOT CONTAIN NUMBERS)
24- HALT_PIN = board .D21 # Halt button GPIO pin (other end to GND)
25- LED_PIN = board .D5 # Status LED pin (v2 Pi cam lacks built-in LED)
22+ INTERVAL = 15 # Time between captures, in seconds
23+ WIDTH = 1280 # Image width in pixels
24+ HEIGHT = 720 # Image height in pixels
25+ QUALITY = 51 # JPEG image quality (0-100)
26+ DEST = "/home/pi/timelapse" # Destination directory (MUST NOT CONTAIN NUMBERS)
27+ PREFIX = "img" # Image prefix (MUST NOT CONTAIN NUMBERS)
28+ HALT_PIN = board .D21 # Halt button GPIO pin (other end to GND)
29+ LED_PIN = board .D5 # Status LED pin (v2 Pi cam lacks built-in LED)
2630COMMAND = "libcamera-still -n --width {width} --height {height} -q {quality} --thumb none -t 250 -o {outfile}"
27- #COMMAND = "raspistill -n -w {width -h {height} -q {quality} -th none -t 250 -o {outfile}"
28- prevtime = 0 # Time of last capture (0 = do 1st image immediately)
31+ # COMMAND = "raspistill -n -w {width -h {height} -q {quality} -th none -t 250 -o {outfile}"
2932
3033def main ():
31- global prevtime
34+ prevtime = 0 # Time of last capture (0 = do 1st image immediately)
3235 halt = digitalio .DigitalInOut (HALT_PIN )
3336 halt .switch_to_input (pull = digitalio .Pull .UP )
3437 led = digitalio .DigitalInOut (LED_PIN )
3538 led .switch_to_output ()
3639
37- # Create destination directory (if not present)
40+ # Create destination directory (if not present)
3841 os .makedirs (DEST , exist_ok = True )
3942
4043 # Find index of last image (if any) in directory, start at this + 1
4144 files = os .listdir (DEST )
42- numbers = [int (re .search (r'\d+' , f ).group ()) for f in files if f .startswith (PREFIX ) and re .search (r'\d+' , f )]
45+ numbers = [
46+ int (re .search (r"\d+" , f ).group ())
47+ for f in files
48+ if f .startswith (PREFIX ) and re .search (r"\d+" , f )
49+ ]
4350 numbers .sort ()
4451 frame = numbers [- 1 ] + 1 if numbers else 1
4552 currenttime = time .monotonic ()
4653
4754 while True :
48- while time .monotonic () < prevtime + INTERVAL : # Until next image capture time
55+ while time .monotonic () < prevtime + INTERVAL : # Until next image capture time
4956 currenttime = time .monotonic ()
5057 # Check for halt button -- hold >= 2 sec
5158 while not halt .value :
5259 if time .monotonic () >= currenttime + 2 :
5360 led .value = True
54- os .system (' shutdown -h now' )
61+ os .system (" shutdown -h now" )
5562 outfile = f"{ DEST } /{ PREFIX } { frame :05} .jpg"
5663 # echo $OUTFILE
5764 led .value = True
58- os .system (COMMAND .format (width = WIDTH , height = HEIGHT , quality = QUALITY , outfile = outfile ))
65+ os .system (
66+ COMMAND .format (width = WIDTH , height = HEIGHT , quality = QUALITY , outfile = outfile )
67+ )
5968 led .value = False
60- frame += 1 # Increment image counter
61- prevtime = currenttime # Save image cap time
69+ frame += 1 # Increment image counter
70+ prevtime = currenttime # Save image cap time
6271
6372
6473if __name__ == "__main__" :
65- main ()
74+ main ()
0 commit comments