-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharte.py
159 lines (127 loc) · 5.26 KB
/
arte.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
from dataclasses import dataclass, field
from enum import Enum
import time
import typing
import cv2
import numpy as np
import pyautogui
import pyperclip
import mouse
class Mode(Enum):
SCANLINE = "scanline",
CONTOUR = "contour"
@dataclass
class Canvas:
width: int = field(default=0)
height: int = field(default=0)
color_x: int = field(default=0)
color_picker_x: int = field(default=0)
color_picker_y: int = field(default=0)
class Albedo:
def __init__(self, canvas: Canvas) -> None:
self.canvas = canvas
self.mouse = mouse.Mouse()
def load_image(self,
path: str) -> cv2.typing.MatLike:
return cv2.imread(path)
def apply_filters(self,
img: cv2.typing.MatLike) -> cv2.typing.MatLike:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return img
def resize_and_crop(self,
img: cv2.typing.MatLike) -> cv2.typing.MatLike:
width = img.shape[1]
height = img.shape[0]
w_factor = width / self.canvas.width
h_factor = height / self.canvas.height
w_factor = 1 / w_factor if w_factor > 1 else 1 + (1 - w_factor)
h_factor = 1 / h_factor if h_factor > 1 else 1 + (1 - h_factor)
return cv2.resize(img,
None,
fx=h_factor,
fy=h_factor,
interpolation=cv2.INTER_LANCZOS4)
def find_contours(self,
img: cv2.typing.MatLike) -> tuple[typing.Sequence[cv2.typing.MatLike], cv2.typing.MatLike]:
edges = cv2.Canny(img, 200, 290, apertureSize=3)
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
return contours # type: ignore
def draw_contours(self,
img: cv2.typing.MatLike,
contours: typing.Any) -> cv2.typing.MatLike:
imagem_branca = np.zeros_like(img)
return cv2.drawContours(image=imagem_branca,
contours=contours,
contourIdx=-1,
color=(255, 255, 255),
thickness=cv2.FILLED)
def show(self, img: cv2.typing.MatLike) -> None:
cv2.imshow("display", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def prepare_image(self,
img: cv2.typing.MatLike) -> tuple[cv2.typing.MatLike,
tuple[typing.Sequence[cv2.typing.MatLike], cv2.typing.MatLike]]:
img = self.apply_filters(img=img)
img = self.resize_and_crop(img=img)
contours = self.find_contours(img=img)
img = self.draw_contours(img=img, contours=contours)
return (img, contours)
def follow_contours(self,
contours: tuple[typing.Sequence[cv2.typing.MatLike], cv2.typing.MatLike],
offset: tuple[int, int] = (0, 0)):
for contour in contours:
shapes = contour.reshape(-1, 2).tolist() # type: ignore
if len(shapes) <= 8:
continue
for shape in shapes:
self.mouse.drag(initial=(int(offset[0] + int(shape[0])),
int(offset[1] + int(shape[1]))))
self.mouse.release_drag()
def follow_scanline(self,
img: cv2.typing.MatLike,
offset: tuple[int, int] = (0, 0)):
height = img.shape[0]
width = img.shape[1]
for y in range(height):
for x in range(width):
pixel = img[y, x]
if pixel == 255:
self.mouse.move_to(coord=(offset[0] + x, offset[1] + y))
self.mouse.click()
def select_color(self,
color: tuple[int, int, int]) -> None:
self.mouse.move_to((self.canvas.color_picker_x, self.canvas.color_picker_y))
self.mouse.click()
time.sleep(0.01)
self.mouse.move_to((560, 1186))
self.mouse.click()
pyautogui.write(str(color[0]))
time.sleep(0.01)
self.mouse.move_to((650, 1186))
self.mouse.click()
pyautogui.write(str(color[1]))
time.sleep(0.01)
self.mouse.move_to((720, 1186))
self.mouse.click()
pyautogui.write(str(color[2]))
def draw(self,
image_path: str,
mode: Mode = Mode.SCANLINE) -> None:
image = self.load_image(path=image_path)
image, contours = self.prepare_image(img=image)
self.show(image)
time.sleep(3)
if mode.value == 'contour':
self.follow_contours(contours=contours,
offset=self.mouse.position())
else:
self.follow_scanline(img=image,
offset=self.mouse.position())
if __name__ == '__main__':
albedo = Albedo(canvas=Canvas(width=1303,
height=729,
color_picker_x=619,
color_picker_y=918))
albedo.draw(image_path="./images/howdy.jpg",
mode=Mode.CONTOUR)