1
+ #!/usr/bin/env python3
2
+
3
+ import os
4
+ import re
5
+ import time
6
+ import board
7
+ import digitalio
8
+
9
+ # Timelapse script, because timelapse options in raspistill don't power
10
+ # down the camera between captures. Script also provides a camera busy LED
11
+ # (v2 cameras don't include one) and a system halt button.
12
+ # 'gpio' command requires WiringPi: sudo apt-get install wiringpi
13
+ # Limitations: if DEST is FAT32 filesystem, max of 65535 files in directory;
14
+ # if DEST is ext4 filesystem, may have performance issues above 10K files.
15
+ # For intervals <2 sec, better just to use raspistill's timelapse feature.
16
+
17
+ # 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)
26
+ 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)
29
+
30
+ def main ():
31
+ global prevtime
32
+ halt = digitalio .DigitalInOut (HALT_PIN )
33
+ halt .switch_to_input (pull = digitalio .Pull .UP )
34
+ led = digitalio .DigitalInOut (LED_PIN )
35
+ led .switch_to_output ()
36
+
37
+ # Create destination directory (if not present)
38
+ os .makedirs (DEST , exist_ok = True )
39
+
40
+ # Find index of last image (if any) in directory, start at this + 1
41
+ 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 )]
43
+ numbers .sort ()
44
+ frame = numbers [- 1 ] + 1 if numbers else 1
45
+ currenttime = time .monotonic ()
46
+
47
+ while True :
48
+ while time .monotonic () < prevtime + INTERVAL : # Until next image capture time
49
+ currenttime = time .monotonic ()
50
+ # Check for halt button -- hold >= 2 sec
51
+ while not halt .value :
52
+ if time .monotonic () >= currenttime + 2 :
53
+ led .value = True
54
+ os .system ('shutdown -h now' )
55
+ outfile = f"{ DEST } /{ PREFIX } { frame :05} .jpg"
56
+ # echo $OUTFILE
57
+ led .value = True
58
+ os .system (COMMAND .format (width = WIDTH , height = HEIGHT , quality = QUALITY , outfile = outfile ))
59
+ led .value = False
60
+ frame += 1 # Increment image counter
61
+ prevtime = currenttime # Save image cap time
62
+
63
+
64
+ if __name__ == "__main__" :
65
+ main ()
0 commit comments