This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
coordinate_calculator.py
60 lines (49 loc) · 1.81 KB
/
coordinate_calculator.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
def find_part_that_can_fit_to_window(
window_size: (int, int),
image_size: (int, int)) -> (float, float, float, float):
window_x, window_y = window_size
image_x, image_y = image_size
factor_x = image_x / window_x
factor_y = image_y / window_y
factor = min(factor_x, factor_y)
image_center_x = image_x / 2
image_center_y = image_y / 2
crop_x = window_x * factor
crop_y = window_y * factor
return \
image_center_x - crop_x / 2, \
image_center_y - crop_y / 2, \
image_center_x + crop_x / 2, \
image_center_y + crop_y / 2
def zoom(
window_size: (int, int),
image_size: (int, int)) -> (int, int):
factor = max(__factor(window_size, image_size))
image_x, image_y = image_size
return int(image_x * factor), int(image_y * factor)
def scale(
window_size: (int, int),
image_size: (int, int)) -> (int, int):
factor = min(__factor(window_size, image_size))
image_x, image_y = image_size
return int(image_x * factor), int(image_y * factor)
def center_box(
window_size: (int, int),
image_size: (int, int)) -> (int, int, int, int):
window_x, window_y = window_size
image_x, image_y = image_size
window_center_x, window_center_y = window_x / 2, window_y / 2
image_center_x, image_center_y = image_x / 2, image_y / 2
return \
int(window_center_x - image_center_x), \
int(window_center_y - image_center_y), \
int(window_center_x + image_center_x), \
int(window_center_y + image_center_y)
def __factor(
window_size: (int, int),
image_size: (int, int)) -> (int, int):
window_x, window_y = window_size
image_x, image_y = image_size
factor_x = window_x / image_x
factor_y = window_y / image_y
return factor_x, factor_y