Class 13 - Studio Day 2
This week:
- project check-ins
- Glue code and Scripting!
Script 1
Methodology:
- Trigger (Bash) script every time we take photo
- Create filename based on date and time
- Take a photo and save under that filename with .jpg suffix
- Pause
- Print to receipt
#!/bin/bash
FILENAME=$(echo $(date -I"seconds").jpg)
echo $FILENAME
libcamera-still -o $FILENAME
sleep 5
#code for receipt printer goes here
Programmatically adding to website
A “hack” to write to server where we can’t deploy node or backend.
#!/bin/sh
name=$(date '+%Y-%m-%d-%H-%M-%S')
libcamera-still -o $name.jpg
echo "<img src='$name.jpg'>">>index.html
- Will save photos by date-time
- Appends html code for the image to our index page, taking advantage that the ‘broken’ html code will still load fine.
- This is a hack! It can be refined and refactored.
Python script
A starter code recipe
#!/usr/bin/python
from picamera2 import Picamera2
picam2 = Picamera2()
# take a photo
picam2.start_and_capture_file("test.jpg")
Python script with date-time filenaming
#!/usr/bin/python
import time
from picamera2 import Picamera2
picam2 = Picamera2()
#create name with YYYY-MM-DD-HH-MM-SS.jpg format
time_date_name = time.strftime("%Y-%m-%d-%H-%M-%S")+".jpg"
#take photo
picam2.start_and_capture_file(time_date_name)
#print("photo capture: " + time_date_name)
Example output
A successful photo