-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
128 lines (94 loc) · 3.46 KB
/
util.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
from PIL import Image
def add_gaussian_noise(img, sigma):
#add gaussian noise
if sigma > 0:
noise = np.random.normal(scale=sigma , size=img.shape).astype(np.float32)
noisy_img = (img + noise).astype(np.float32)
else:
noisy_img = img.astype(np.float32)
#noisy_img=np.clip(noisy_img, 0.0, 1.0)
return noisy_img
def np_to_pil(img_np):
'''Converts image in np.array format to PIL image.
From C x W x H [0..1] to W x H x C [0...255]
'''
ar = np.clip(img_np*255,0,255).astype(np.uint8)
if img_np.shape[0] == 1:
ar = ar[0]
else:
ar = ar.transpose(1, 2, 0)
return Image.fromarray(ar)
def load(path):
"""Load PIL image."""
img = Image.open(path)
return img
def get_bernoulli_mask(for_image, zero_fraction=0.9):
img_mask_np=(np.random.random_sample(size=pil_to_np(for_image).shape) > zero_fraction).astype(int)
img_mask = np_to_pil(img_mask_np)
return img_mask
def get_image(path, imsize=-1):
"""Load an image and resize to a specific size.
Args:
path: path to image
imsize: tuple or scalar with dimensions; -1 for `no resize`
"""
img = load(path)
if isinstance(imsize, int):
imsize = (imsize, imsize)
if imsize[0]!= -1 and img.size != imsize:
if imsize[0] > img.size[0]:
img = img.resize(imsize, Image.BICUBIC)
else:
img = img.resize(imsize, Image.ANTIALIAS)
img_np = pil_to_np(img)
return img, img_np
def pil_to_np(img_PIL):
'''Converts image in PIL format to np.array.
From W x H x C [0...255] to C x W x H [0..1]
'''
ar = np.array(img_PIL)
if len(ar.shape) == 3:
ar = ar.transpose(2,0,1)
else:
ar = ar[None, ...]
return ar.astype(np.float32) / 255.
def np_to_pil(img_np):
'''Converts image in np.array format to PIL image.
From C x W x H [0..1] to W x H x C [0...255]
'''
ar = np.clip(img_np*255,0,255).astype(np.uint8)
if img_np.shape[0] == 1:
ar = ar[0]
else:
ar = ar.transpose(1, 2, 0)
return Image.fromarray(ar)
def load_LR_HR_imgs_sr(LR_image,HR_image, imsize, factor, enforse_div32=None):
# load images for SR (LR,HR)
img_orig_pil, img_orig_np = get_image(HR_image, -1)
if imsize != -1:
img_orig_pil, img_orig_np = get_image(HR_image, imsize)
img_HR_pil, img_HR_np = img_orig_pil, img_orig_np
LR_size = [
img_HR_pil.size[0] // factor,
img_HR_pil.size[1] // factor
]
img_LR_pil, img_LR_np = get_image(LR_image, -1)
# IF only have HR images
# img_LR_pil = img_HR_pil.resize(LR_size, Image.ANTIALIAS)
img_LR_np = pil_to_np(img_LR_pil)
print('HR and LR resolutions: %s, %s' % (str(img_HR_pil.size), str (img_LR_pil.size)))
return {
'orig_pil': img_orig_pil,
'orig_np': img_orig_np,
'LR_pil': img_LR_pil,
'LR_np': img_LR_np,
'HR_pil': img_HR_pil,
'HR_np': img_HR_np
}
def get_noisy_image(img_np, sigma):
#Adds Gaussian noise to an image.
img_noisy_np = np.clip(img_np + np.random.normal(scale=sigma, size=img_np.shape), 0, 1).astype(np.float32)
#img_noisy_np = (img_np + np.random.normal(scale=sigma, size=img_np.shape)).astype(np.float32)
img_noisy_pil = np_to_pil(img_noisy_np)
return img_noisy_pil, img_noisy_np