Skip to main content
. 2025 May 21;11(5):376. doi: 10.3390/gels11050376
import cv2
import os
def create_timelapse(input_folder, output_file, fps):
    # Verify if the input folder exists
    if not os.path.exists(input_folder):
        print(f”Error: The directory {input_folder} does not exist.”)
        return
    # Get list of image files in the folder
    images = [img for img in os.listdir(input_folder) if img.endswith(“.jpg”) or img.endswith(“.png”)]
        if not images:
        print(f“Error: No image files found in the directory {input_folder}.”)
        return
        images.sort() # Sort the images by filename
    # Read the first image to get the frame size
    first_image_path = os.path.join(input_folder, images[15])
    frame = cv2.imread(first_image_path)
    if frame is None:
        print(f“Error: Unable to read the first image file {first_image_path}.”)
        return
    height, width, layers = frame.shape
    # Initialise the video writer
    fourcc = cv2.VideoWriter_fourcc(*’XVID’)
    video = cv2.VideoWriter(output_file, fourcc, fps, (width, height))
    # Add each image to the video
    for image in images:
        image_path = os.path.join(input_folder, image)
        frame = cv2.imread(image_path)
        if frame is None:
            print(f“Warning: Unable to read the image file {image_path}. Skipping.”)
            continue
        video.write(frame)
    # Release the video writer
    video.release()
    print(f“Timelapse video saved as {output_file}”)
# Parameters
input_folder = r” # Replace with your folder path
output_file =” # Output video file name
fps = 30 # Frames per second
# Print the input folder path to verify it
print(f“Using input folder: {input_folder}”)
# Create the timelapse video
create_timelapse(input_folder, output_file, fps)