-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
230 lines (182 loc) · 5.46 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from PIL import Image
from progress.bar import Bar
from pyglet.math import Mat4
import numpy as np
import os
import os.path
import time
COLOR_CHANNELS = 3
MAX_COLOR = 255
COLOR_BLACK = np.zeros(3)
def open_image(img_filename):
img = Image.open(img_filename)
img_arr = np.array(img)
return img_arr
def normalize(arr):
"""
Normalize a vector using numpy.
Args:
arr (ndarray): Input vector
Returns:
ndarray: Normalized input vector
"""
norm = np.linalg.norm(arr)
if norm == 0:
return arr
return arr / norm
def distance(p1, p2):
"""
Get the distance between points p1 and p2
Args:
p1(ndarray): Point 1
p2(ndarray): Point 2
Returns:
float: Distance
"""
dist = np.linalg.norm(p1 - p2)
return dist
def humanize_time(secs):
minutes, secs = divmod(secs, 60)
hours, minutes = divmod(minutes, 60)
return '%02d:%02d:%02d' % (hours, minutes, secs)
def degrees2radians(degrees):
return (degrees / 360) * 2 * np.pi
def radians2degrees(radians):
return (radians / (2 * np.pi)) * 360
def normalize_color(color):
return color / MAX_COLOR
def lerp(a, b, t):
return t * a + (1 - t) * b
def blerp(u, v, img_arr):
if len(img_arr.shape) == 3:
height, width, _ = img_arr.shape
else:
height, width = img_arr.shape
x = u * width
y = v * height
return sample_2d(x, y, img_arr)
def sample_2d(x, y, img_arr):
if len(img_arr.shape) == 3:
height, width, _ = img_arr.shape
else:
height, width = img_arr.shape
# Flip y value to go from top to bottom
y = height - y
# Interpolate values of pixel neighborhood of x and y
i = int(np.round(x))
j = int(np.round(y))
if i == 0 or j == 0 or i >= width or j >= height:
if i >= width:
i -= width
if j >= height:
j -= height
return img_arr[j][i]
# t and s are interpolation parameters that go from 0 to 1
t = x - i + 0.5
s = y - j + 0.5
# Bilinear interpolation
color = (
img_arr[j - 1][i - 1] * (1 - t) * (1 - s)
+ img_arr[j - 1][i] * t * (1 - s)
+ img_arr[j][i - 1] * (1 - t) * s
+ img_arr[j][i] * t * s
)
return color
def resize(img_arr, ratio):
# resize img_arr with 2D
if len(img_arr.shape) == 3:
h, w, _ = img_arr.shape
else:
h, w = img_arr.shape
new_h = h * ratio
new_w = w * ratio
new_img_arr = np.zeros([new_h, new_w])
for j in range(new_h):
for i in range(new_w):
new_img_arr[j][i] = img_arr[j // ratio][i // ratio]
return new_img_arr
def menu_str(options):
s = "Select an option:\n"
for opt_num, opt in enumerate(options, start=1):
s += f"[{opt_num}] for {opt}\n"
s += "[0] to exit\n"
return s
def exist_or_create(dir_path):
"""
Check if the given directory path exist, if not creates it
Args:
dir_path(string): The path for the directory
"""
if not os.path.exists(dir_path):
os.mkdir(dir_path)
def create_texture_from_noise(
color, size, noise_img_path, dark_color=COLOR_BLACK
):
"""
Create a 2D image texture by multiplying a given color with a grayscale
noise.
Args:
color(ndarray): RGB color
size(int): length of one side in pixels
noise_img_path(str): Path for the noise image
dark_color(ndarray): RGB for dark color to interpolate
Returns:
Image: 2D image texture
"""
noise_img = Image.open(noise_img_path)
if noise_img.size[0] != size:
noise_img = noise_img.resize([size, size])
noise_arr = np.asarray(noise_img) / MAX_COLOR
combined_array = noise_arr * color + (1 - noise_arr) * dark_color
final_array = np.array(combined_array, dtype=np.uint8)
output_img = Image.fromarray(final_array)
return output_img
def create_light_and_dark_image(img_path):
img = Image.open(img_path)
dark_img = img.point(lambda p: p * 0.6)
light_img = img.point(lambda p: p * 1.3)
dark_img.save('C0.png')
light_img.save('C1.png')
class Timer:
def __init__(self):
self.start_time = 0
self.end_time = 0
self.elapsed_time = 0
def start(self):
self.start_time = time.time()
def stop(self):
self.end_time = time.time()
self.elapsed_time = self.end_time - self.start_time
def __str__(self):
return humanize_time(self.elapsed_time)
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def to_dict(self):
return {
'x': float(np.round(self.x, 3)),
'y': float(np.round(self.y, 3)),
'z': float(np.round(self.z, 3))
}
@staticmethod
def dict_to_arr(point_dict):
arr = np.array([point_dict['x'], point_dict['y'], point_dict['z']])
return arr
class ProgressBar(Bar):
suffix = '%(percent)d%% [%(elapsed_td)s / %(eta_td)s]'
check_tty = False
class OrthographicView:
def __init__(self, window):
self.window = window
def __enter__(self):
self.view = self.window.view
self.projection = self.window.projection
self.window.view = Mat4()
self.window.projection = Mat4.orthogonal_projection(
0, self.window.width, 0, self.window.height, -255, 255
)
def __exit__(self, exc_type, exc_val, exc_tb):
self.window.view = self.view
self.window.projection = self.projection