How to Create a Time-Lapse Video with a Raspberry Pi Camera

Discover how to set up a Raspberry Pi camera for automated time-lapse photography with Python. In this comprehensive guide, we’ll walk you through the entire process — from choosing the right hardware and installing necessary software to writing Python scripts and configuring your setup. By the end of this tutorial, you’ll have everything you need to create beautiful time-lapse videos that can be captured anywhere. Start capturing those perfect moments with your Raspberry Pi today!
For todays example I made a simple timelapse of a city skyline, not the best angle but I hope you understand the gist of what we will be producing.
What you will need:
Raspberry Pi Model 4B, Power Supply, SD Card
Raspberry Pi Camera Module V2–8
Raspberry Pi Camera Holder (Optional)
— — -
Before we delve into the topic, we invite you to support our ongoing efforts and explore our various platforms dedicated to enhancing your IoT projects:
  • Subscribe to our YouTube Channel: Stay updated with our latest tutorials and project insights by subscribing to our channel at YouTube — Shilleh.
  • Support Us: Your support is invaluable. Consider buying me a coffee at Buy Me A Coffee to help us continue creating quality content.
  • Hire Expert IoT Services: For personalized assistance with your IoT projects, hire me on UpWork.
ShillehTek Website (Exclusive Discounts):
ShillehTekAmazon Store:

1-) Install the Required Libraries

We need to install flask, opencv, and picamera2 using the apt installer on our Raspberry Pi. Go into a terminal and run the following commands.

sudo apt update
sudo apt install python3-opencv python3-flask python3-picamera2 ffmpeg
 

2-) Timelapse Code

Now that you have the packages installed go ahead and create a Python file with the following content, running this code will generate the timelapse, make sure it is stationary when running to have a smooth lapse.”Suggestion: “Create a Python file with the following content. Running this code will generate the timelapse. Make sure the camera is stationary during capture to ensure a smooth timelapse.

import cv2
from datetime import datetime
import time
import os
from picamera2 import Picamera2
# Initialize the camera
camera = Picamera2()
camera.configure(camera.create_preview_configuration(main={"format": 'XRGB8888', "size": (1920, 1080)}))
camera.start()

# Timelapse settings
capture_interval = 10 # seconds between each frame
duration = 60 * 60 * 25 # duration of the timelapse capture in seconds (25 hours for this example)

# Create a new output directory with a timestamp
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_dir = f"timelapse_frames_{timestamp}"
os.makedirs(output_dir, exist_ok=True)

start_time = time.time()

while time.time() - start_time < duration:
frame = camera.capture_array()

# Get the current time in hour:minute format
current_time = datetime.now().strftime("%H:%M")

# Define the position and font for the time overlay
position = (frame.shape[1] - 200, 50) # Adjust the position (x, y)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
color = (0, 0, 0) # Black color
thickness = 3

# Overlay the time on the frame
cv2.putText(frame, current_time, position, font, font_scale, color, thickness, cv2.LINE_AA)

# Save frame to disk
filename = f"{output_dir}/frame_{int(time.time())}.jpg"
cv2.imwrite(filename, frame)

# Wait for the next capture
time.sleep(capture_interval)

camera.stop()

High Level Overview:

Import Libraries: The script uses cv2 for image processing, datetime for timestamping, time for handling intervals, os for file operations, and Picamera2 to control the Raspberry Pi camera.

import cv2 
from datetime import datetime
import time
import os
from picamera2 import Picamera2

Initialize and Configure the Camera: An instance of Picamera2 is created, configured for preview mode with a specific format and resolution, and started.

camera = Picamera2()
camera.configure(camera.create_preview_configuration(main={"format": 'XRGB8888', "size": (1920, 1080)}))
camera.start()

Set Timelapse Parameters: The capture interval is set to 10 seconds, and the total duration is set to 25 hours (in seconds).

capture_interval = 10  # seconds
duration = 60 * 60 * 25 # 25 hours

Create Output Directory: A unique output directory is created using the current timestamp to store captured frames.

timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_dir = f"timelapse_frames_{timestamp}"
os.makedirs(output_dir, exist_ok=True)

Capture Frames in a Loop: The script enters a loop that runs until the specified duration is reached. It captures a frame, overlays the current time, saves the frame to disk, and waits for the next interval.

while time.time() - start_time < duration:
frame = camera.capture_array()
current_time = datetime.now().strftime("%H:%M")
cv2.putText(frame, current_time, (frame.shape[1] - 200, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3, cv2.LINE_AA)
filename = f"{output_dir}/frame_{int(time.time())}.jpg"
cv2.imwrite(filename, frame)
time.sleep(capture_interval)

Stop the Camera: After capturing all frames, the camera is stopped to free up resources.

camera.stop()

This script captures images at regular intervals, overlays the current time, and saves them, allowing you to compile these frames into a time-lapse video later.

3-) Test Camera Angle with Flask App (Optional)

If you’d like to preview the camera angle before capturing your timelapse, you can set up a live stream using Flask. This step is optional but recommended for first-time setups.

Create another Python file with the following code and run it.

from flask import Flask, Response
from picamera2 import Picamera2
import cv2
from datetime import datetime

app = Flask(__name__)
camera = Picamera2()
camera.configure(camera.create_preview_configuration(main={"format": 'XRGB8888', "size": (1920, 1080)}))
camera.start()

def generate_frames():
while True:
frame = camera.capture_array()

# Get the current time in hour:minute format
current_time = datetime.now().strftime("%H:%M")

# Define the position and font for the time overlay
position = (frame.shape[1] - 200, 50) # Adjust the position (x, y)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
color = (255, 255, 255) # White color
thickness = 2

# Overlay the time on the frame
cv2.putText(frame, current_time, position, font, font_scale, color, thickness, cv2.LINE_AA)

# Encode the frame as a JPEG image
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()

# Yield the frame to the response stream
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)

If your Raspberry Pi is connected to your local WiFi you should be able to access the camera view through the following link.

http://<Your Raspberry Pi IP>:5000/video_feed

You can get your IP address of your Raspberry Pi by entering a terminal on the Raspberry Pi and typing the command ifconfig. You can then find the IP address in the inet section.

Once this is done you will see a video stream in your chrome browser!

Adjust the camera angle as you see fit before initiating the time-lapse code so you do not regret running a bad-angled time lapse so long.

Eventually I set up my camera by taping it to my Raspberry Pi and angling it with a Tripod, there are much more professional ways of doing this but I had to get creative since I did not have a special camera case.

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.