-
Notifications
You must be signed in to change notification settings - Fork 0
/
gifwriter.py
executable file
·35 lines (28 loc) · 1.05 KB
/
gifwriter.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
import subprocess
import os
from scipy.misc import imsave
class GifWriter(object):
def __init__(self, temp_format, dest_gif):
try:
subprocess.check_call(
['convert'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
raise Exception('imagemagick is required for gif support')
except subprocess.CalledProcessError:
pass
self.temp_format = temp_format
self.dest_gif = dest_gif
self.temp_filenames = []
self.closed = False
def append(self, image):
if self.closed:
raise Exception('GifWriter is already closed')
filename = self.temp_format % len(self.temp_filenames)
self.temp_filenames.append(filename)
imsave(filename, image)
def close(self):
subprocess.check_call(['convert', '-delay', '2', '-loop', '0'] +
self.temp_filenames + [self.dest_gif])
for filename in self.temp_filenames:
os.unlink(filename)
self.closed = True