/
utils.py
61 lines (45 loc) · 1.62 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
49
50
51
52
53
54
55
56
57
58
59
60
import os
import shutil
import numpy as np
from skimage.transform import resize
def safe_makedirs(dirs):
if not isinstance(dirs, list):
dirs = [dirs]
for dir_name in dirs:
if not os.path.exists(dir_name):
os.makedirs(dir_name)
def safe_deldirs(dirs):
if not isinstance(dirs, list):
dirs = [dirs]
for dir_name in dirs:
if os.path.isdir(dir_name):
shutil.rmtree(dir_name)
def resize_pad(image, new_shape, fill=0):
"""
Resizes an image while maintaining aspect ratio, appropriately padding borders
:param image: image to resize (either 2D grayscale image or 3D RGB image)
:param new_shape: (w, h) of output image
:param fill: value to pad edges with
:return: output image of size (w, h) and same aspect ratio as input image
"""
new_h, new_w = new_shape
if len(image.shape) == 2:
h, w = image.shape
else:
h, w, c = image.shape
f_xy = min(new_w / w, new_h / h)
h, w = int(h * f_xy), int(w * f_xy)
scaled = resize(image, (h, w), preserve_range=True, anti_aliasing=True, mode='constant').astype(np.uint8)
if len(scaled.shape) == 3:
scaled_h, scaled_w, _ = scaled.shape
else:
scaled_h, scaled_w = scaled.shape
dw, dh = new_w - scaled_w, new_h - scaled_h
x, y = int(np.floor(dw / 2)), int(np.floor(dh / 2))
if len(image.shape) == 3:
output = np.ones((new_h, new_w, c)) * fill
output[y:y + scaled_h, x:x + scaled_w, :] = scaled
else:
output = np.ones((new_h, new_w)) * fill
output[y:y + scaled_h, x:x + scaled_w] = scaled
return output