Skip to content

Instantly share code, notes, and snippets.

@NuclearPhoenixx
Last active February 13, 2025 20:31
Show Gist options
  • Save NuclearPhoenixx/32dd657c5549f3b155aaf3bf599dfdea to your computer and use it in GitHub Desktop.
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.
#!/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