Introduction
Timelapse photography is a photographic technique of taking a sequence of frames at set intervals to record changes that take place slowly over time. When the frames are shown at normal speed the action seems much faster.
The images are then stitched together into a video to produce an animated sequence of images.
In this tutorial, we explain how to build a Timelapse-Taking Machine with Raspberry Pi, a camera and a Python program.
Hardware Required
- Raspberry Pi 4 4GB / 8GB
- Raspberry Pi Camera 5MP or Raspberry Pi Camera 8MP
- Raspberry Pi 4 Power Supply 15.3W USB-C
- Micro SD Card 32GB Class 10
- Raspberry Pi 4.3 inch DSI Interface Capacitive Touch Display with Case 800x480 (optional)
- Wireless Keyboard with Touchpad (optional)
Connecting the Hardware
Insert the Micro SD Card that contains the Raspberry Pi OS into the Raspberry Pi.
Mount the Raspberry Pi into the Raspberry Pi 4.3" HDMI Capacitive Touch Display. Refer to the YouTube Video and the product website for detailed setup instructions.
Connect the pi camera into the camera slot on the Raspberry Pi. The blue tape on the cable should be facing towards the Ethernet port.
Connect the Power Supply, a mouse, and a keyboard to the Raspberry Pi. Power on your setup and you are ready to start setting up the software!
Setting up the Software
Turn on the Raspberry Pi. Open the Raspberry Pi Configuration.
Enable the camera.
Save and restart the Raspberry Pi. To check if you have connected and set up the camera correctly, open the terminal from the taskbar and type "raspistill -o /home/pi/Desktop/image.jpg".
You should see a new image file appearing on the desktop.
Coding the Timelapse Program
Let's start coding our timelapse project. We will be coding this program in Python. Open "Thonny Python IDE".
from picamera import PiCamera from time import sleep from os import system import datetime
The lines of codes above give the program access to the camera and date/time libraries necessary to capture images at various intervals.
#set the time to run the timelapse runningMinutes = 10
#delay between each photo taken in seconds sleepDelay = 1 #total number of photos to take based on running minutes and sleep delay totalPhotos = int((runningMinutes*60)/sleepDelay)
The timelapse program is configured to run for 10 minutes. During this time, it will capture 1 image every second.
#create camera object camera = PiCamera() #set the image resolution camera.resolution = (1024, 768)
The camera instance is created and set to take photos at a resolution of 1024x768.
#create a loop to take multiple images for i in range(totalPhotos): #capture image and name it in a 4-digit sequence camera.capture('/home/pi/Pictures/image{0:04d}.jpg'.format(i)) #wait before taking next image sleep(sleepDelay)
A loop is defined to capture images every second and save these images in the folder "/home/pi/Pictures".
#get the current datetime dateraw= datetime.datetime.now() #format datetime into year, month, date, hour, minute datetimeformat = dateraw.strftime("%Y-%m-%d_%H:%M") #use ffmpeg to create video from images system('ffmpeg -r {} -f image2 -s 1024x768 -nostats -loglevel 0 -pattern_type glob -i "/home/pi/Pictures/*.jpg" -vcodec libx264 -crf 25 -pix_fmt yuv420p /home/pi/Videos/{}.mp4'.format(30, datetimeformat))
The program finds out the current date/time. It then performs a stitching routine to stitch all the images that it has captured into one video. It names this video based on the date/time where it performed this stitching routine.
Click on “Run”. Save the file as “timelapse”. You should see “>>> %Run timelapse.py”. Go into the pictures folder, you will be able to see the images slowly coming in.
Check the videos folder to see your new timelapse video.
Double-click the video to watch it.
Resources
- Download the full source code: Click Here