1
+ # SPDX-FileCopyrightText: 2024 Melissa leBlanc-Williams for Adafruit Industries
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
1
5
#!/usr/bin/env python3
2
6
3
7
import os
15
19
# For intervals <2 sec, better just to use raspistill's timelapse feature.
16
20
17
21
# 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)
26
30
COMMAND = "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}"
29
32
30
33
def main ():
31
- global prevtime
34
+ prevtime = 0 # Time of last capture (0 = do 1st image immediately)
32
35
halt = digitalio .DigitalInOut (HALT_PIN )
33
36
halt .switch_to_input (pull = digitalio .Pull .UP )
34
37
led = digitalio .DigitalInOut (LED_PIN )
35
38
led .switch_to_output ()
36
39
37
- # Create destination directory (if not present)
40
+ # Create destination directory (if not present)
38
41
os .makedirs (DEST , exist_ok = True )
39
42
40
43
# Find index of last image (if any) in directory, start at this + 1
41
44
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
+ ]
43
50
numbers .sort ()
44
51
frame = numbers [- 1 ] + 1 if numbers else 1
45
52
currenttime = time .monotonic ()
46
53
47
54
while True :
48
- while time .monotonic () < prevtime + INTERVAL : # Until next image capture time
55
+ while time .monotonic () < prevtime + INTERVAL : # Until next image capture time
49
56
currenttime = time .monotonic ()
50
57
# Check for halt button -- hold >= 2 sec
51
58
while not halt .value :
52
59
if time .monotonic () >= currenttime + 2 :
53
60
led .value = True
54
- os .system (' shutdown -h now' )
61
+ os .system (" shutdown -h now" )
55
62
outfile = f"{ DEST } /{ PREFIX } { frame :05} .jpg"
56
63
# echo $OUTFILE
57
64
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
+ )
59
68
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
62
71
63
72
64
73
if __name__ == "__main__" :
65
- main ()
74
+ main ()
0 commit comments