forked from ColinYu1/cmsc-p3-24fall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
33 lines (29 loc) · 1.04 KB
/
Copy pathutils.py
File metadata and controls
33 lines (29 loc) · 1.04 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
import math
import numpy as np
import sys
from PIL import Image, ImageTk
def length(x):
return math.sqrt(x[0]**2 + x[1]**2)
def angle_bw(x, y):
normx = np.linalg.norm(x)
normy = np.linalg.norm(y)
if normx <= 1e-8 or normy <= 1e-8:
return 0
x = x / np.linalg.norm(x)
y = y / np.linalg.norm(y)
# using cross-product formula
return -math.degrees(math.asin((x[0] * y[1] - x[1] * y[0])/(length(x)*length(y))))
# the dot-product formula, left here just for comparison (does not return angles in the desired range)
# return math.degrees(math.acos((self.a * other.a + self.b * other.b)/(self.length()*other.length())))
def add_noise(x, std):
return x + np.random.normal(0, std)
def load_image(path, scale):
try:
img = Image.open(path)
new_width = int(img.width * float(scale))
new_height = int(img.height * float(scale))
img = img.resize((new_width, new_height), Image.LANCZOS)
return img, ImageTk.PhotoImage(img)
except IOError as e:
print(e)
sys.exit(1)