-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathanimation.py
More file actions
68 lines (48 loc) · 1.55 KB
/
animation.py
File metadata and controls
68 lines (48 loc) · 1.55 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
import copy
import pydot
import glob
import os
import shutil
import imageio
from visualiser import Node, Edge, Graph
class Animation:
def __init__(self):
self.frames = []
self._frame_id = 0
def next_step(self, frame):
self.frames.append(copy.deepcopy(frame))
self._frame_id += 1
@staticmethod
def make_directory():
if not os.path.exists("frames"):
os.makedirs("frames")
def next_frame(self):
pass
def previous_frame(self):
pass
def get_frame(self, frame_id):
return self.frames[frame_id]
def get_frames(self):
return self.frames[::]
def write_frame(self, frame_id):
frame = self.get_frame(frame_id)
dot_graph, *rest = pydot.graph_from_dot_data(frame)
dot_graph.write_png(f"frames/temp_{frame_id}.png")
def write_file(self):
self.make_directory()
for frame_id in range(len(self.get_frames())):
self.write_frame(frame_id)
def write_gif(self, name="out.gif", delay=3):
self.write_file()
images = []
# sort frames images in ascending order to number in image filename
# image filename: frames/temp_1.png
sorted_images = sorted(
glob.glob("frames/*.png"),
key=lambda fn: int(fn.split("_")[1].split(".")[0])
)
for filename in sorted_images:
images.append(imageio.imread(filename))
imageio.mimsave(name, images, duration=delay)
# Delete temporary directory
shutil.rmtree("frames")