Skip to content

Instantly share code, notes, and snippets.

@abachman
Last active June 6, 2017 21:37
Show Gist options
  • Save abachman/b41e7da0607ea8a00c7aae6a6e67bc98 to your computer and use it in GitHub Desktop.
Save abachman/b41e7da0607ea8a00c7aae6a6e67bc98 to your computer and use it in GitHub Desktop.
Raspberry Pi rolling timelapse setup using basic debian distro + https://www.adafruit.com/product/3099
<!doctype html>
<html>
<head>
<style>
html, body { height: 100%; }
body {
padding: 0;
background: #000;
}
.Aligner {
display: flex;
height: 100%;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div class="Aligner">
<!-- <img src='latest.jpeg' /> -->
<video width="1024" height="768" loop controls>
<source src="" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<script>
// cache busting
document.querySelector('video source').src = 'latest.mp4?' + Math.random();
</script>
</body>
</html>
import io
import time
import picamera
import base64
from PIL import Image
camera = picamera.PiCamera()
camera.resolution = (1020, 768)
# Start a preview and let the camera warm up for 2 seconds
camera.start_preview()
time.sleep(2)
image_stream = io.BytesIO()
# capture a raw image directly to a buffer
camera.capture(image_stream, format='jpeg', resize=(1024, 768))
image_stream.seek(0)
value = base64.b64encode(image_stream.read())
# open buffer as a PIL object - https://python-pillow.org/
image_stream.seek(0)
image = Image.open(image_stream, 'r')
# save optimized image to new buffer
optim_85_stream = io.BytesIO()
image.save(optim_85_stream, format='jpeg', quality=85, optimize=True)
optim_85_stream.seek(0)
# spit out raw bytes for use in CLI pipe
print(optim_85_stream.read())
#!/bin/bash
REMOTE='issecret'
REMOTE_DIR='toomanysecrets'
function log() {
ts=`date +'%Y-%m-%d %H:%M:%S %Z'`
echo "[$ts] $@" >> /home/pi/projects/optimize-jpeg/snap.log
}
log "SNAPPING"
# add new
epoch=`date +'%s'`
log " SENDING TO $REMOTE:$REMOTE_DIR/office/snap-$epoch.jpeg"
python /home/pi/projects/optimize-jpeg/main.py | ssh $REMOTE "cat > $REMOTE_DIR/office/snap-$epoch.jpeg"
# cleanup all files more than two hours old
log " CLEANING OLD FILES"
ssh $REMOTE "find $REMOTE_DIR/office/*.jpeg -type f -mmin +120 -exec rm {} \;"
# generate video file
log " REGENERATING VIDEO"
ssh $REMOTE "cd $REMOTE_DIR && ffmpeg -y -r 10 -f image2 -s 1024x768 -pattern_type glob -i 'office/snap-*.jpeg' latest.mp4"
log " DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment