-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotteno_gif.py
100 lines (80 loc) · 2.65 KB
/
rotteno_gif.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from rotteno import rotteno
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
class RottenoFrameGenerator:
def __init__(self, input_grid, num_frames):
self.curr_frame = 0
self.num_frames = num_frames
self.rotteno = rotteno(input_grid)
def __iter__(self):
return self
def __next__(self):
self.curr_frame += 1
if self.curr_frame < self.num_frames:
self.advance_frame()
return self.rotteno.grid
raise StopIteration
def advance_frame(self):
self.rotteno.update_grid()
def get_grid(self):
return self.rotteno.grid
def RottenoFrameTest(grid):
from helper import rotteno_colormap as cmap
num_minutes = rotteno(grid).turns_until_rot() + 1
if num_minutes == 0:
num_minutes = 10
rotten = RottenoFrameGenerator(grid, num_minutes)
for idx, img in enumerate(rotten):
plt.imshow(img, cmap=cmap)
plt.title(f"{idx} minutes")
plt.axis('off')
plt.show()
def RottenGIF(grid, file_name):
from helper import rotteno_colormap as cmap
num_minutes = rotteno(grid).turns_until_rot() + 1
if num_minutes == 0:
num_minutes = 10
rotten = RottenoFrameGenerator(grid, num_minutes)
fig = plt.figure()
ax = plt.axes()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
im = plt.imshow(grid, interpolation='none', cmap=cmap)
def animate(idx):
if idx == 0:
fig.suptitle(f'Day Zero')
im.set_array(rotten.get_grid())
return [im]
else:
fig.suptitle(f'Day {idx}')
rotten.advance_frame()
im.set_array(rotten.get_grid())
return [im]
anim = FuncAnimation(fig, animate, frames=num_minutes, interval=1000)
anim.save(file_name, dpi=100, writer='ffmpeg')
# plt.show()
def random_grid(x_dim, y_dim):
import random
p_rotten = .2
p_fresh = p_rotten + .6
grid = [[0 for _ in range(x_dim)] for _ in range(y_dim)]
for j in range(x_dim):
for i in range(y_dim):
rand_float = random.uniform(0, 1)
if rand_float < p_rotten:
grid[i][j] = 2
elif p_rotten < rand_float < p_fresh:
grid[i][j] = 1
else:
grid[i][j] = 0
return grid
if __name__ == "__main__":
import os
path = os.getcwd() + "\\output"
x_dim = 25
y_dim = 25
grid = random_grid(x_dim, y_dim)
print("Generating MP4")
RottenGIF(grid, path + "\\rotteno.mp4")
print("Generating GIF")
os.system(f"ffmpeg -y -i {path}\\rotteno.mp4 {path}\\rotteno.gif")