365 day time lapse with a raspberry pi

I set out to make a time lapse video covering a year of foliage changes in the garden of the apartment block where I live. I used a Raspberry Pi with a camera module.

It took six photos each day, and at the same times each day. 12pm, 12.30pm, 1pm, 1.30pm, 2pm, 2.30pm. My thinking was that I’d pick the best photo for each day. In the end I chose images of one time slot instead. The photos are then merged into a video.

https://youtu.be/PX6UJ-aZOW0

This post details the making of the video, mostly for me to have a record of the commands and settings I used.

Enclosure

Its vitally important that the camera module doesn’t move at all over the course of the time lapse. For this, I made an enclosure for the Pi to be installed in the window aperture. The camera module is positioned behind a hole in the side.Enclosure opened with Pi and camera module
Enclosure closed with camera opening visible

Enclosure installed in the window aperture

Setup and networking

I wanted the Pi connected to my home WiFi network in order to communicate with it, and for it to be internet enabled for progress emails. I used a WiFi dongle for this and mostly just followed the instructions here: Automatically Connect a Raspberry Pi to a Wifi Network

Taking a photo

The built-in raspistill command is used. Support for the camera module is enabled when the Pi is first setup with raspi-config.

The script that takes the photo looks like this:

#!/bin/bash
#Filename: photo.sh

DATE=$(date +"%Y%m%d_%H%M")
raspistill -o /home/pi/images/$DATE.jpg

This takes a photo and stores it in an images directory with a filename formatted with the date and time of the image.

Make thumbnails of the daily photos

I wanted a daily email of thumbnails of the photos taken. Part of the imagemagick suite of tools is convert which does this.

sudo apt-get install imagemagick

#!/bin/bash
#Filename: thumbnail.sh

DATE=$(date +"%Y%m%d")
SRC_DIR=/home/pi/images
TRG_DIR=/home/pi/thumbnails
IMAGES="`echo "$SRC_DIR"/"$DATE"_*.jpg`"
convert $IMAGES -strip -thumbnail 7% -append $TRG_DIR/$DATE.jpg

Here, a set of photos is stripped of anything redundant, reduced in size, sequenced top-to-bottom and a copy stored in another directory.

Backup the daily photos to a USB drive

Just in case anything went wrong I wanted a backup copy of the photos. For this I used a USB drive permanently inserted into the Pi and used rsync as the easiest way to copy only new files.

sudo apt-get install rsync

#!/bin/bash
#Filename: backup.sh

DEST=/mnt/usb
if ! /bin/mountpoint -q $DEST; then
sudo mount -o uid=pi,gid=pi /dev/sda1 $DEST
fi
if /bin/mountpoint -q $DEST; then
/usr/bin/rsync -auW /home/pi/images $DEST
fi

Here, if the destination is not already a mount point, the drive will be mounted. Then the contents of the images directory gets rsync‘ed to the mounted drive. The drive will stay mounted until the Pi is switched off.

Local storage capacity considerations

With each photo being about 4.5Mb, 6 photos a day for a year would lead to running out of space both on the Pi’s internal storage as well as the USB backup drive. So I wanted to determine daily the current disk usage. The df command was used.

#!/bin/bash
#Filename: df.sh

/bin/df -h > /home/pi/df.txt

Here, the human readable output is sent to a local file for a later scheduled email.

Periodically I would then manually ssh in and copy the images to another computer and free up space on the Pi and the USB drive.

Daily emails

I wanted the day’s thumbnails emailed to me everyday as confirmation that it’s all working correctly. ssmtp does the emailing and mpack enables attachments to emails.

sudo apt-get install ssmtp mpack

The email sending needs a mail server. Strangely, sending to a gmail address using the gmail server wasn’t working. I’d get permission and authentication errors. However I did succeed sending to a gmail address using a yahoo account for the server. The ssmtp config looks like this.

cat /etc/ssmtp/sstmp.conf

root=my_address@yahoo.com
mailhub=smtp.mail.yahoo.com:587
rewriteDomain=yahoo.com
UseSTARTTLS=YES
AuthMethod=LOGIN
AuthUser=my_username
AuthPass=my_password
FromLineOverride=YES

The actual emailing an attachment looks like this:

#!/bin/bash
#Filename: email.sh

DATE=$(date +"%Y%m%d")
EMAIL=my_address@gmail.com
/usr/bin/mpack -s "Daily thumbnail" -d /home/pi/df.txt /home/pi/thumbnails/$DATE.jpg $EMAIL

Here the body of the email is made from the contents of the file df.txt, described above, and the attachment is the thumbnail image, having a filename formatted with today’s date.

Additional help: Send emails with attachments from the Linux command line

Scheduling

Putting it all together into cronjobs gave me a crontab listing as follows:

crontab -l

# m h dom mon dow command
0,30 12-14 * * * /home/pi/scripts/photo.sh
0  15 * * * /home/pi/scripts/thumbnail.sh
5  15 * * * /home/pi/scripts/backup.sh
29 15 * * * /home/pi/scripts/df.sh
30 15 * * * /home/pi/scripts/email.sh

Here, a photo is taken on the hour and half-hour between 12pm and 2.30pm. At 3pm a thumbnail would be made, at 3.15pm the backup would be made on the USB drive. The current disk usage would be saved to file a minute before being sent as an email together with the thumbnail as an attachment.

Merge into a video

After a year of the above I had all the needed photos. To accumulate images into a video I used ffmpeg, this time on a Linux VirtualBox instance.

sudo apt-get install ffmpeg

I copied all the same time slot images to their own directories and renamed them with a sequential identifier. ie. I’d have all the 12pm photos in one directory named 1200_1.jpg, 1200_2.jpg, 1200_3.jpg … 1200_365.jpg, etc. The same for the 12.30pm, 1pm, 1.30pm, etc. photos.

ffmpeg -r 15 -start_number 1 -i 1200_%d.jpg -s 1280x960 -vcodec libx264 ../garden_1200.mp4

Here, the images with filenames starting with identifier 1 are stitched together with a frame rate of 15, a 1280×960 size and x264 encoding and sent to an output .mp4 file.

Conclusion

I was expecting the changing foliage over the seasons to be the dominant feature. Instead I find the changing length of the shadows over the seasons to be more impressive.

Disappointing is the flicker of shadows that occurs between overcast days and sunshiny days. On overcast days there are no shadows from the trees. When followed by a bright day having the tree shadows, then the flicker caused by shadows disappearing and appearing again causes the flicker.

Post-processing won’t help, it’s not the contrast or brightness at fault, it’s the lack of shadows. I thought there’d be sufficient variation in the six photos each day for me to choose the brightest one, but typically if there are no tree shadows in one photo, there won’t be any in others of that day.

A fun project.