-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmist_utils.py
More file actions
157 lines (147 loc) · 3.96 KB
/
mist_utils.py
File metadata and controls
157 lines (147 loc) · 3.96 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import argparse
import PIL
from PIL import Image
import numpy as np
def parse_args():
parser = argparse.ArgumentParser(description="Configs for Mist V1.2")
parser.add_argument(
"-img",
"--input_image_path",
type=str,
default="test/sample.png",
help="path of input image",
)
parser.add_argument(
"--output_name",
type=str,
default="misted_sample",
help="path of saved image",
)
parser.add_argument(
"--output_dir",
type=str,
default="vangogh",
help="path of output dir",
)
parser.add_argument(
"-inp",
"--input_dir_path",
type=str,
default=None,
help="Path of the dir of images to be processed.",
)
parser.add_argument(
"-e",
"--epsilon",
type=int,
default=16,
help=(
"The strength of Mist"
),
)
parser.add_argument(
"-s",
"--steps",
type=int,
default=100,
help=(
"The step of Mist"
),
)
parser.add_argument(
"-in_size",
"--input_size",
type=int,
default=512,
help=(
"The input_size of Mist"
),
)
parser.add_argument(
"-b",
"--block_num",
type=int,
default=1,
help=(
"The number of partitioned blocks"
),
)
parser.add_argument(
"--mode",
type=int,
default=2,
help=(
"The mode of MIST."
),
)
parser.add_argument(
"--rate",
type=int,
default=1,
help=(
"The fused weight under the fused mode."
),
)
parser.add_argument(
"--mask",
default=False,
action="store_true",
help=(
"Whether to mask certain region of Mist or not. Work only when input_dir_path is None. "
),
)
parser.add_argument(
"--mask_path",
type=str,
default="test/processed_mask.png",
help="Path of the mask.",
)
parser.add_argument(
"--non_resize",
default=False,
action="store_true",
help=(
"Whether to keep the original shape of the image or not."
),
)
args = parser.parse_args()
return args
def load_mask(mask):
mask = np.array(mask)[:,:,0:3]
for p in range(mask.shape[0]):
for q in range(mask.shape[1]):
# if np.sum(mask[p][q]) != 0:
# mask[p][q] = 255
if mask[p][q][0] != 255:
mask[p][q] = 0
else:
mask[p][q] = 255
return mask
def closing_resize(image_path: str, input_size: int, block_num: int = 1, no_load: bool = False) -> PIL.Image.Image:
if no_load:
im = image_path
else:
im = Image.open(image_path)
target_size = list(im.size)
resize_parameter = min(target_size[0], target_size[1])/input_size
block_size = 8 * block_num
target_size[0] = int(target_size[0] / resize_parameter) // block_size * block_size
target_size[1] = int(target_size[1] / resize_parameter) // block_size * block_size
img = im.resize(target_size)
return img, target_size
def load_image_from_path(image_path: str, input_width: int, input_height: int = 0, no_load: bool = False) -> PIL.Image.Image:
"""
Load image form the path and reshape in the input size.
:param image_path: Path of the input image
:param input_size: The requested size in int.
:returns: An :py:class:`~PIL.Image.Image` object.
"""
if input_height == 0:
input_height = input_width
if no_load:
img = image_path.resize((input_width, input_height),
resample=PIL.Image.BICUBIC)
else:
img = Image.open(image_path).resize((input_width, input_height),
resample=PIL.Image.BICUBIC)
return img