Last active
February 13, 2025 20:31
-
-
Save NuclearPhoenixx/32dd657c5549f3b155aaf3bf599dfdea to your computer and use it in GitHub Desktop.
Raspberry Pi Python script that shoots a single photo with a filename corresponding to the time and date. Can be called over and over in a timelapse context.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import time | |
import subprocess | |
from datetime import datetime | |
def has_already_restarted(): | |
return os.path.exists("/tmp/camera_restart_status") | |
def mark_as_restarted(): | |
with open("/tmp/camera_restart_status", "w") as f: | |
f.write("restarted") | |
def capture_image(): | |
try: | |
timestamp = int(time.time()) | |
output_path = f"./timelapse/{timestamp}.jpg" | |
command = [ | |
"libcamera-still", "--output", output_path, "--awb", "indoor", "--hdr", "auto", "-q", "100", | |
"--height", "1440", "--width", "2560", "--brightness", "0.1" | |
] | |
result = subprocess.run(command, check=True, capture_output=True, text=True) | |
print(f"Success: {output_path}") | |
except subprocess.CalledProcessError as e: | |
print(f"Error: {e.stderr}") | |
return False | |
return True | |
def main(): | |
if not capture_image(): | |
if has_already_restarted(): | |
print("Error: Camera still not working, no longer rebooting.") | |
else: | |
print("Error! Rebooting Pi...") | |
mark_as_restarted() | |
os.system("sudo reboot") | |
else: | |
if has_already_restarted(): | |
os.remove("/tmp/camera_restart_status") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment