Skip to content

Instantly share code, notes, and snippets.

@kiri-thornalley
Created May 6, 2025 09:21
Show Gist options
  • Save kiri-thornalley/36de158ebb664ecab1beab83dfb7573d to your computer and use it in GitHub Desktop.
Save kiri-thornalley/36de158ebb664ecab1beab83dfb7573d to your computer and use it in GitHub Desktop.
Figure to animated gif
import imageio.v2 as imageio
import os
import cv2
# Load and sort PNG files
image_folder = "path/to/image/folder"
images = sorted([os.path.join(image_folder, file) for file in os.listdir(image_folder) if file.endswith(".png")])
# Read the first image to get dimensions
first_image = cv2.imread(images[0])
height, width, _ = first_image.shape
# Read images and resize if necessary
frames = []
for img_path in images:
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert OpenCV's BGR to RGB
img_resized = cv2.resize(img, (width, height)) # Ensure same size
frames.append(img_resized)
# Save as animated PNG
output_path = "/path/to/output"
imageio.mimsave(output_path, frames, format="GIF", duration=900, loop=0) # duration in ms
print(f"Animated PNG saved as {output_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment