-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreategif.py
162 lines (125 loc) · 4.28 KB
/
creategif.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import imageio
import subprocess
from typing import List, Union
import random
#Pulled code below from pygifsicle -- a wraper for gifsicle
################################################################################
def gifsicle(sources: Union[List[str], str], destination: str = None,
optimize: bool = False, colors: int = 256, options: List[str] = None):
"""Apply gifsickle with given options to image at given paths.
Parameters
-----------------
sources:Union[List[str], str],
Path or paths to gif(s) image(s) to optimize.
destination:str=None
Path where to save updated gif. By default the old image is overwrited.
optimize:bool=False,
Boolean flag to add the option to optimize image.
colors:int=256,
Integer value representing the number of colors to use. Must be a power of 2.
options:List[str]=None
List of options.
Raises
------------------
ValueError:
If given source path does not exist.
ValueError:
If given source path is not a gif image.
ValueError:
If given destination path is not a gif image.
References
------------------
You can learn more about gifsicle at the project home page:
https://www.lcdf.org/gifsicle/
"""
if isinstance(sources, str):
sources = [sources]
if any([not os.path.exists(source) for source in sources]):
raise ValueError("Given source path does not exists.")
if any([not source.endswith(".gif") for source in sources]):
raise ValueError("Given source path is not a gif image.")
if destination is None:
destination = sources[0]
if not destination.endswith(".gif"):
raise ValueError("Given destination path is not a gif image.")
if options is None:
options = []
if optimize and "--optimize" not in options:
options.append("--optimize")
subprocess.call(["gifsicle", *options, *sources, "--colors",
str(colors), "--output", destination])
def optimize(source: str, *args, **kwargs):
"""Optimize given gif.
Parameters
-----------------
source:str,
Path to gif image to optimize.
"""
gifsicle(source, *args, **kwargs, optimize=True)
################################################################################
def pics2gif(dir_path, out_name, out_path, duration, img_type):
"""
Convert set of pictures to gif
------------------------------
dir_path: str
path to directory where images are stored -- only looks at png
out_name: str
name of output file -- without .gif
out_path: str
location where gif will be output
duration: str
duration of single iteration of gif
img_type: str
type of images used to make gif
"""
pics = []
for pic_name in os.listdir(dir_path):
if pic_name.endswith(img_type):
p_path = os.path.join(dir_path,pic_name)
pic = imageio.imread(p_path)
pics.append(pic)
out_name = out_name + ".gif"
out_path = os.path.join(out_path,out_name)
imageio.mimsave(out_path,pics,duration=float(duration)/len(pics))
optimize_gif(out_path=out_path)
def vid2gif(vid_path, out_name, out_path, duration, start, end, choppy = 1):
"""
Convert video to gif
------------------------------
vid_path: str
path to video -- must be mp4
out_name: str
name of output file -- without .gif
out_path: str
location where gif will be output
duration: str
duration of single iteration of gif
start: str
start time of gif
end: str
end time of gif
choppy: str
how choppy the gif will be -- lower input -> more choppy
"""
out_name = out_name + ".gif"
out_path = os.path.join(out_path,out_name)
reader = imageio.get_reader(vid_path)
fps = reader.get_meta_data()['fps']
start_frame = int(fps*float(start))
end_frame = int(fps*float(end))
#writer = imageio.get_writer(out_path, fps=fps)
pics = []
for i,im in enumerate(reader):
if(start_frame<=i and i<=end_frame):
if(random.uniform(0,1)<=choppy):
pics.append(im)
#writer.append_data(im)
#writer.close()
imageio.mimsave(out_path,pics,duration=float(duration)/len(pics))
optimize_gif(out_path=out_path)
def optimize_gif(out_path):
"""
optimize the gif -- usually cuts size in half
"""
optimize(out_path)