-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathutils.py
34 lines (28 loc) · 1 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
import scipy.misc, numpy as np, os, sys
import imageio
from PIL import Image
def save_img(out_path, img):
img = np.clip(img, 0, 255).astype(np.uint8)
imageio.imwrite(out_path, img)
def scale_img(style_path, style_scale):
scale = float(style_scale)
o0, o1, o2 = imageio.imread(style_path, pilmode='RGB').shape
scale = float(style_scale)
new_shape = (int(o0 * scale), int(o1 * scale), o2)
style_target = _get_img(style_path, img_size=new_shape)
return style_target
def get_img(src, img_size=False):
img = imageio.imread(src, pilmode='RGB') # misc.imresize(, (256, 256, 3))
if not (len(img.shape) == 3 and img.shape[2] == 3):
img = np.dstack((img,img,img))
if img_size != False:
img = np.array(Image.fromarray(img).resize(img_size[:2]))
return img
def exists(p, msg):
assert os.path.exists(p), msg
def list_files(in_path):
files = []
for (dirpath, dirnames, filenames) in os.walk(in_path):
files.extend(filenames)
break
return files