-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_video_maker.py
More file actions
78 lines (52 loc) · 2.59 KB
/
algorithm_video_maker.py
File metadata and controls
78 lines (52 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import os
# To animate the images we first need to grab them
# we use the standard format of f_0,f_1,f_2,...,f_i,...,f_n to get the images
def get_images(filepath : str) -> list[np.ndarray]:
# We store the images in a simple list, we don't need more complex structures for this
images : list[np.ndarray] = []
# First we will get all the images in the folder and then store them in the program
# All frames have a frame number, starting from 0 and incrementing
i : int = 0
while True:
this_image_filepath = filepath + f"/f_{i}.png"
if os.path.exists(this_image_filepath):
# Get the next image and add it to our list of images
next_image = plt.imread(this_image_filepath)
images.append(next_image)
i += 1
else: break
return images
# Create a video
def make_video(filename : str, filepath : str, fps : int = 1, seconds_per_image : float = 2 ) -> None:
# First we need to actually get hold of the images to animate
images : list[np.ndarray] = get_images(filepath)
# SETTING UP THE FIGURE so it's the same size as the original images were
# 100 dpi for this computer
dpi=100
height, width, _ = images[0].shape
# What size does the figure need to be in inches to fit the image?
figsize = width / float(dpi), height / float(dpi)
# Create a figure of the right size with one axes that takes up the full figure
fig = plt.figure(figsize=figsize)
ax = fig.add_axes([0, 0, 1, 1])
# Hide spines, ticks, etc.
ax.axis('off')
# Determine for how many frames each image should be on the screen for
frame_count_per_image = ( fps * seconds_per_image )
# The function that will be used to generate each frame of the video
def animate_video(t):
# Clear the previous elements on the screen to avoid lag
plt.cla()
# Hide spines, ticks, etc.
ax.axis('off')
# Get the image to show for this frame
img = images[int(t / frame_count_per_image)]
# Actually show the image on the plot
plt.imshow(img)
# Create the animation itself using FuncAnimation
anim = FuncAnimation(fig, animate_video, frames = int(frame_count_per_image * len(images)))
# Save the animation to the filepath with the given filename by the user
anim.save(filepath + "/" + filename, fps=fps, dpi=dpi)