forked from MingtaoGuo/SinGAN_Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
40 lines (37 loc) · 1.39 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
from PIL import Image
import numpy as np
def get_training_imgs(path, scale_factor=4/3):
img = np.array(Image.open(path), dtype=np.uint8)
h, w = img.shape[0], img.shape[1]
if h > w:
w_ = 250 * w // h
max_img = np.array(Image.fromarray(img).resize([w_, 250]), dtype=np.uint8)
else:
h_ = 250 * h // w
max_img = np.array(Image.fromarray(img).resize([250, h_]), dtype=np.uint8)
if h < w:
w_ = 25 * w // h
min_img = np.array(Image.fromarray(img).resize([w_, 25]), dtype=np.uint8)
else:
h_ = 25 * h // w
min_img = np.array(Image.fromarray(img).resize([25, h_]), dtype=np.uint8)
min_h, min_w = min_img.shape[0], min_img.shape[1]
max_h, max_w = max_img.shape[0], max_img.shape[1]
heights = []
width = []
max_hw = min_h if min_h > min_w else min_w
n = 0
while max_hw * scale_factor ** n < 250:
heights.append(min_h * scale_factor ** n)
width.append(min_w * scale_factor ** n)
n += 1
heights.append(max_h)
width.append(max_w)
imgs = []
for h, w in zip(heights, width):
imgs.append(np.array(Image.fromarray(img).resize([int(w), int(h)]), dtype=np.uint8))
return imgs
# imgs = get_training_imgs("D:/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/JPEGImages/000046.jpg")
# for img in imgs:
# Image.fromarray(img).show()
# a = 0