-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
143 lines (110 loc) · 4.76 KB
/
image.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
from PIL import Image, ImageDraw
from pyautogui import locateAllOnScreen, screenshot, center
from config import *
from operator import itemgetter
def show_found(image: Image, file: str):
image_draw = ImageDraw.Draw(image)
i = 1
for location in locateAllOnScreen(file, confidence=0.94):
cords = [location[0], location[1], location[0] + location[2], location[1] + location[3]]
print(cords[1])
image_draw.rectangle(cords)
image_draw.text((location[0] - 25, location[1] - 5), str(i), font=font)
i += 1
image.show()
def get_elements_with_rectangles(image: Image):
image_draw = ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", 20)
elements = []
j = 1
used_y = []
for i, reference in enumerate(references_table):
for location in locateAllOnScreen(reference['img'], confidence=reference['confidence'], grayscale=True):
cords = center(location)
print(cords.y)
print(used_y)
if not used_y:
used_y.append(cords.y)
closest_y = min(used_y, key=lambda a: abs(a - cords.y))
if (closest_y - 5) < cords.y < (closest_y + 5):
cords_y = closest_y
else:
cords_y = cords.y
used_y.append(cords.y)
print(cords_y)
print()
element = {'id': i,
'x': cords.x,
'y': cords_y}
if element not in elements:
elements.append(element)
cords = [location[0], location[1], location[0] + location[2], location[1] + location[3]]
image_draw.rectangle(cords)
image_draw.text((location[0] - 25, location[1] - 5), str(j), font=font)
j += 1
# if len(elements) not in elements_size:
# raise ValueError (f'Найдено {len(elements)} элементов')
elements = sorted(elements, key=itemgetter('y', 'x'))
print(elements)
print(len(elements))
elements_small = []
j = 1
for i, reference in enumerate(references_reqs):
for location in locateAllOnScreen(reference['img'], confidence=reference['confidence']):
cords = center(location)
element = {'id': i,
'x': cords.x,
'y': cords.y}
elements_small.append(element)
cords = [location[0], location[1], location[0] + location[2], location[1] + location[3]]
image_draw.rectangle(cords, outline='red')
image_draw.text((location[0] - 25, location[1] - 5), str(j), font=font)
j += 1
elements_small = sorted(elements_small, key=itemgetter('y', 'x'))
print(elements_small)
print(len(elements_small))
image.show()
def get_elements(table: bool):
references = references_table if table else references_reqs
elements = []
used_y = []
for i, reference in enumerate(references):
for location in locateAllOnScreen(reference['img'], confidence=reference['confidence'], grayscale=True):
cords = center(location)
if not used_y:
used_y.append(cords.y)
closest_y = min(used_y, key=lambda a: abs(a - cords.y))
if (closest_y - 5) < cords.y < (closest_y + 5):
cords_y = closest_y
else:
cords_y = cords.y
used_y.append(cords.y)
element = {'id': i,
'x': cords.x,
'y': cords_y}
if element not in elements:
elements.append(element)
if len(elements) not in elements_size and table:
from log import show_notification
show_notification('Ошибка', f'Найдено {len(elements)} элементов')
raise ValueError (f'Найдено {len(elements)} элементов')
elements = sorted(elements, key=itemgetter('y', 'x'))
for element in elements:
print(element)
print(len(elements))
return elements
def show_path(line):
image = screenshot()
image_draw = ImageDraw.Draw(image)
for i in range(len(line)-1):
if i % 2 == 0:
image_draw.polygon((line[i]['x']+5, line[i]['y'], line[i]['x']-5, line[i]['y'], line[i+1]['x'], line[i+1]['y']), fill='white')
else:
image_draw.polygon((line[i]['x'], line[i]['y']+5, line[i]['x'], line[i]['y']-5, line[i+1]['x'], line[i+1]['y']), fill='white')
image_draw.text((line[i]['x'], line[i]['y']), str(i + 1), font=font)
image_draw.text((line[-1]['x'], line[-1]['y']), str(len(line)), font=font)
image.show()
if __name__ == '__main__':
# get_elements(screenshot())
get_elements_with_rectangles(screenshot())
# show_found(screenshot(), 'assets/55.png')