forked from carpedm20/visual-analogy-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
48 lines (38 loc) · 1.25 KB
/
utils.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
import pprint
import numpy as np
import scipy.misc
from time import gmtime, strftime
pp = pprint.PrettyPrinter()
strfnow = lambda: strftime("%Y-%m-%d %H:%M:%S", gmtime())
def merge(*images):
images = list(images)
# For difference between target and inferenced image
# images.append(abs(images[-2] - images[-1]))
h, w = images[0].shape[1], images[0].shape[2]
h_count, w_count = len(images), len(images[0])
img = np.zeros((h * h_count, w * w_count, 3))
for idx, image_set in enumerate(zip(*(images))):
for jdx, image in enumerate(image_set):
copy_img = image.copy()
copy_img[[0,-1],:,:]=1
copy_img[:,[0,-1],:]=1
img[jdx*h:jdx*h + h, idx*w:idx*w + w, :] = copy_img
return img
def imsave(path, image):
print(" [*] Save %s" % path)
image[image>1] = 1
image[image<0] = 0
return scipy.misc.imsave(path, image)
def make_gif(images, fname, duration=2, true_image=False):
import moviepy.editor as mpy
def make_frame(t):
try:
x = images[int(len(images)/duration*t)]
except:
x = images[-1]
if true_image:
return x.astype(np.uint8)
else:
return ((x+1)/2*255).astype(np.uint8)
clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_gif(fname, fps = len(images) / duration)