This repository has been archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanimation.py
77 lines (60 loc) · 2.21 KB
/
animation.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
############################################
# Generate animations from figures
############################################
#import#
import os
import numpy as np
from subprocess import call
import moviepy.video.io.ImageSequenceClip
from joblib import Parallel, delayed
################################################################################
print('\t')
print('---------------------------------------------')
print('- Welcome to animation utilities for CUMC3D -')
print('---------------------------------------------')
print('\t')
################################################################################
# definine function #
def getfname(x):
x = x.split('-')
x = x[0] + '-' + x[1]
return x
#sorting function #
def keyfunc(x):
test = x.split('-')
test = test[2].split('.png')
test = float(test[0])
return test
################################################################################
#define path#
path = './figure/'
#load unique filename in figure folder#
filename = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".png"):
filename.append(os.path.join(file))
#get model names#
filename = [getfname(x) for x in filename]
filename = np.unique(filename)
################################################################################
# generate animation
#frame per second#
fps=10
#now loop over all unique png file#
def process(i):
image_files = []
image_files = [os.path.join(path,img)
for img in os.listdir(path)
if (img.startswith(filename[i]) and img.endswith(".png"))]
image_files = sorted(image_files, key=keyfunc)
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)
clip.write_videofile('./movie/'+filename[i]+'.mp4')
# do it parallely #
Parallel(n_jobs=10)(delayed(process)(i) for i in range(len(filename)))
################################################################################
print('---------------------------')
print('- End animation generator -')
print('---------------------------')
print('\t')
################################################################################