forked from elleryqueenhomels/fast_neural_style_transfer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (43 loc) · 1.44 KB
/
utils.py
File metadata and controls
64 lines (43 loc) · 1.44 KB
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
# Utility
import numpy as np
from os import listdir, mkdir, sep
from os.path import join, exists, splitext
from scipy.misc import imread, imsave, imresize
def list_images(directory):
images = []
for file in listdir(directory):
name = file.lower()
if name.endswith('.png'):
images.append(join(directory, file))
elif name.endswith('.jpg'):
images.append(join(directory, file))
elif name.endswith('.jpeg'):
images.append(join(directory, file))
return images
def get_images(paths, height=None, width=None):
if isinstance(paths, str):
paths = [paths]
images = []
for path in paths:
image = imread(path, mode='RGB')
if height is not None and width is not None:
image = imresize(image, [height, width], interp='nearest')
images.append(image)
images = np.stack(images, axis=0)
return images
def save_images(paths, datas, save_path, prefix=None, suffix=None):
if isinstance(paths, str):
paths = [paths]
assert(len(paths) == len(datas))
if not exists(save_path):
mkdir(save_path)
if prefix is None:
prefix = ''
if suffix is None:
suffix = ''
for i, path in enumerate(paths):
data = datas[i]
name, ext = splitext(path)
name = name.split(sep)[-1]
path = join(save_path, prefix + name + suffix + ext)
imsave(path, data)