5147+ reviews
Order by 16:00 for same day shipping
14 days return
GB
EN
Individual
Business
In this project you will learn how to make your own time-lapse camera with your raspberry pi 4 or raspberry pi 5. In this project everything is explained so that it is easy to do yourself and also easy to adjust to what is needed. We will talk about how you can configure the time-lapse completely yourself and it is also explained how the functions in the code work. Let's get started right away!
sudo poweroff
or button). sudo raspi-config
sudo apt-get update -y sudo apt-get upgrade -y
In the terminal, type:
mkdir ~/timelapse_project
mkdir
creates a new directory.~
represents your home directory (e.g. /home/pi
).Go to that folder:
cd ~/timelapse_project
cd
= change directory, you “go to” the folder.Check that you are in the correct folder:
pwd
pwd
shows your current location in the file system./home/pi/timelapse_project
.config.yaml
and explanationOpen the editor:
nano config.yaml
nano
is a simple text editor in the terminal. Paste this content:
camera:
preview: true # Show a small preview window while recording
pixel_ratio:
- 1920 # Width of the photos
- 1080 # Height of the photos
preview_pixel_ratio:
- 640 # Width of the preview
- 480 # Height of the preview
logging: true # Set to true to see status messages in the terminal
picture_folder: "pictures" # Folder where pictures will go
time_lapse_folder: "time_lapse_videos" # Folder where the video will go
maxtime:
duration: 60 # Total recording time in seconds
frame_interval: 0.5 # Between each photo (in seconds)
frame_rate_video: 30 # Frames per second in the video
Save : Press Ctrl+O, Enter.
Close : Ctrl+X
What does this do?
Config.yaml is also the configuration file for all the python code you will run. You tell the script how the camera should work, where the files should be located, how long it will run, and how fast the final video will be.
dep.bash
)Create the file:
nano dep.bash
Paste this in:
#!/usr/bin/env bash sudo apt-get update -y # Haalt de nieuwste pakketlijsten op sudo apt-get upgrade -y # Installeert alle beschikbare updates sudo apt-get autoremove -y # Verwijdert oude, overbodige pakketten # Installeer Python-modules & camera-drivers sudo apt-get install python3-picamera2 -y sudo apt-get install python3-libcamera -y sudo apt-get install python3-yaml -y sudo apt-get install python3-shutil -y sudo apt-get install python3-opencv -y # 'cv2' sudo apt-get install python3-numpy -y sudo apt-get install python3-pathlib -y
Make executable:
chmod +x dep.bash
Execute:
./dep.bash
What's happening?
update
andupgrade
ensure that your system is up to date.autoremove
cleans up.install
fetches specific Python packages and camera libraries.
timelapse.py
) and code explanationOpen a new file:
nano timelapse.py
Paste the complete code ( from picamera2 import Picamera2
to cleanup()
).
from picamera2 import Picamera2
from libcamera import Transform, controls
import libcamera
import yaml
import shutil
import cv2
import numpy as np
from pathlib import Path
import os
import time
# Open het configuratie bestand en lees hem
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
# Maak een camera object
camera = Picamera2()
# Zet maximale stappen
iterations = int(config["maxtime"]["duration"] / config["frame_interval"]) # maximale tijd / interval
picdir = config["picture_folder"]
log = config["logging"]
def images_to_video(image_folder=f"{config['picture_folder']}", output_path=f"{config['time_lapse_folder']}/output.mp4", fps=30, pixel_radius=3):
if log:
print("Foto's naar video aan het converteren...")
folder_path = Path(image_folder)
# Get list of numbered images
image_files = sorted([f for f in folder_path.glob('*.jpg')
if f.stem.isdigit()],
key=lambda x: int(x.stem))
if not image_files:
raise ValueError("Geen fotos gevonden")
# Read first image to get dimensions
first_frame = cv2.imread(str(image_files[0]))
# Create VideoWriter object with H.264 codec
height, width = first_frame.shape[:2]
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # H.264 codec
out = cv2.VideoWriter(
str(output_path),
fourcc,
fps,
(width, height)
)
# Process each image
for img_path in image_files:
frame = cv2.imread(str(img_path))
# Apply Gaussian blur based on pixel radius
if pixel_radius > 0:
frame = cv2.GaussianBlur(frame,
(pixel_radius*2 + 1, pixel_radius*2 + 1),
0)
out.write(frame)
# Release VideoWriter
out.release()
cv2.destroyAllWindows()
def create_directorys(folders: list):
for folder in folders:
if not os.path.exists(f"{os.getcwd()}/{folder}"):
try:
os.mkdir(f"{os.getcwd()}/{folder}")
print(f"Map aangemaakt : {folder}")
except Exception as e:
if e.errno != errno.EEXIST:
print(f"Error bij het aanmaken van een map: {e}")
return
def cleanup():
shutil.rmtree(f"{os.getcwd()}/{config['picture_folder']}") # verwijdert alle foto bestanden
exit()
def maak_foto_en_sla_op(i):
try:
# Maak de foto
camera.capture_file(f"{os.getcwd()}/{picdir}/{i}.jpg")
if log:
print(f"foto {i} opgeslagen")
except Exception as e:
raise e
def error(e):
images_to_video()
cleanup()
print(f"Er is een error voorgekomen ({e}). De fotos die zijn gemaakt zijn opgeslagen en worden omgezet in een time-lapse")
exit(1)
# configureer de code
camera_config = camera.create_still_configuration(
main={"size": (config["camera"]["pixel_ratio"][0], config["camera"]["pixel_ratio"][1])},
lores={"size": (config["camera"]["preview_pixel_ratio"][0], config["camera"]["preview_pixel_ratio"][1])}
)
camera_config = camera.create_preview_configuration(
main={"size": (config["camera"]["pixel_ratio"][0], config["camera"]["pixel_ratio"][1])},
lores={"size": (config["camera"]["preview_pixel_ratio"][0], config["camera"]["preview_pixel_ratio"][1])}
)
camera.configure(camera_config)
camera.start(show_preview=config["camera"]["preview"])
camera.set_controls({"AfMode": controls.AfModeEnum.Continuous, "AfSpeed": controls.AfSpeedEnum.Fast})
create_directorys([config["picture_folder"], config["time_lapse_folder"]])
print("Let op : Het bestand output.mp4 word overgeschreven ook al staat hij er. Als je nog een oude video heb sla hem dan onder een andere naam op of op een andere plek om hem nog te behouden")
if log:
print("Programma begint met opnemen over 3 seconden")
time.sleep(3)
if log:
print("Programma maakt nu foto's")
try:
for i in range(1, iterations+1):
try:
maak_foto_en_sla_op(i)
time.sleep(config["frame_interval"])
except Exception as e:
error(e)
cleanup()
except Exception as e:
error(e)
cleanup()
images_to_video()
print("Klaar!")
cleanup()
Save and Exit : Ctrl+O, Enter & Ctrl+X.
from picamera2 import Picamera2 import yaml, shutil, cv2, numpy as np from pathlib import Path import os, time
Gets all the modules we need.
with open('config.yaml','r') as f: config = yaml.safe_load(f)
Reads your settings from config.yaml
.
Calculating iterations
iterations = int(config["maxtime"]["duration"]/ config["frame_interval"])
Calculates the total number of photos taken.
Create folders
def create_directorys(folders): # Maakt folders als ze nog niet bestaan
Ensures that pictures/
and time_lapse_videos/
exist.
def maak_foto_en_sla_op(i): camera.capture_file(f"{picdir}/{i}.jpg")
Takes a single photo and saves it as 1.jpg
, 2.jpg
, etc.
def images_to_video(...): # Leest alle .jpg-bestanden, maakt er een mp4 van
Combines the photos and (optionally) adds blur.
def error(e): images_to_video() # Zet op dat er een video wordt gemaakt cleanup()
Catches errors, ensures that photos taken are not lost.
camera.start(...) for i in range(1, iterations+1): maak_foto_en_sla_op(i) time.sleep(config["frame_interval"]) images_to_video() cleanup()
In your timelapse_project
folder you should now see:
ls -R
. ├── config.yaml ├── dep.bash ├── timelapse.py ├── pictures/ # (leegt aanvankelijk) └── time_lapse_videos/ # (leegt aanvankelijk)
cd ~/timelapse_project
). python3 timelapse.py
What you see:
Wait until Klaar!
appears in the terminal.
After completion the timelapse is in:
~/timelapse_project/time_lapse_videos/output.mp4
On the Pi itself :
omxplayer time_lapse_videos/output.mp4
On your computer: transfer the file via SCP/USB and open in VLC or another player.
Problem | Solution |
---|---|
Camera not available | Check sudo raspi-config → Interface Options → Camera |
Permission error | Run script with python3 (not as root), and check file perms |
Low disk space | Adjust maxtime or frame_interval , or expand SD card |
Save photos | Remove or comment cleanup() from the end of the script |
Video codec issues | Use omxplayer or adjust fourcc in images_to_video() |