-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert_gif.py
59 lines (54 loc) · 1.47 KB
/
convert_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
'''
convert img_to_gif script
Copyright (c) Xiangzi Dai, 2019
'''
import imageio
import os
import sys
def create_gif(or_path,source, name, duration):
"""
Only support .png format image
"""
frames = []
for img in source:
frames.append(imageio.imread(os.path.join(or_path,img)))
imageio.mimsave(name, frames, 'GIF', duration=duration)
print("ok")
def get_list(file_path):
"""
get file order by file create time
"""
dir_list = os.listdir(file_path)
if not dir_list:
return
else:
dir_list = sorted(dir_list,key=lambda x: os.path.getmtime(os.path.join(file_path, x)))
return dir_list
def main(paths,start=None,end=None):
"""
paths: target dir
"""
pic_list = get_list(paths)
gif_name = "result.gif"
duration_time = 0.2 #duration time
# create gif
create_gif(paths,pic_list[start:end], gif_name, duration_time)
if __name__ == '__main__':
"""
default: all images on ./data_dir
example: python3 convert_gif.py ./data_dir
or
python3 convert_gif.py ./data_dir 80 600
"""
nums = len(sys.argv)
if nums==2:
main(sys.argv[1])
elif nums==3:
main(sys.argv[1],int(sys.argv[2]))
elif nums==4:
if int(sys.argv[2])<=int(sys.argv[3]):
main(sys.argv[1],int(sys.argv[2]),int(sys.argv[3]))
else:
print("start_num should less than end_num")
else:
print("please input vaild param")