-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
379 lines (317 loc) · 11 KB
/
Copy pathapp.py
File metadata and controls
379 lines (317 loc) · 11 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from __future__ import division
from tkinter import Tk, Canvas, Button, PhotoImage, filedialog
from PIL import Image, ImageGrab, ImageTk
from win32gui import FindWindow, GetWindowRect
from keyboard import add_hotkey
from pickle import load
import numpy as np
import pyautogui
# 预设值常量
diff_limit = 400 # 相似颜色最大距离
draw_area = 0.72 # 绘画区域比例
pause_point = 0.06 # 画点时间
pause_line = 0.1 # 画线时间
check_game = True # 是否检测游戏运行
# 屏幕大小 目前只支持全屏1080p
screen_w = 1920
screen_h = 1080
# 运行中变量
img_show = None # 预览图
running = False # 是否正在画图
target_img_path = '' # 用户传入的图片路径
# 获取调色板变量
f = open('./bin/colors_all.bin', 'rb')
colors_all = load(f)
f.close()
f = open('./bin/colors_all_dic.bin', 'rb')
colors_all_dic = load(f)
f.close()
# 计算最接近的颜色
def closest(p):
p = np.array(p)
distances = np.sqrt(np.sum((colors_all - p) ** 2, axis=1))
index_of_smallest = np.where(distances == np.amin(distances))
smallest_distance_color = colors_all[index_of_smallest]
return smallest_distance_color[0]
# 颜色距离
def colors_diff(pixel_a, pixel_b):
return np.square(pixel_a[0] - pixel_b[0]) + np.square(pixel_a[1] - pixel_b[1]) + np.square(pixel_a[2] - pixel_b[2])
# 获取屏幕截图
def get_image():
hwnd = FindWindow(0, 'Draw&Guess')
if hwnd == 0:
return None
else:
(x1, y1, x2, y2) = GetWindowRect(hwnd)
img = ImageGrab.grab((x1, y1, x2, y2))
return img
def start():
# 粗糙画图
point_size = 32 # 画笔大小
reduce_color = True # 减少颜色
color_num = 5 # 颜色数量
max_dot_pitch_multiple = 0 # 最大连线间距倍数
global running
global target_img_path
global colors_all
global colors_all_dic
# 防止多次画图
if running:
return 0
running = True
# 读取图片
if not (target_img_path.endswith('.jpg') or target_img_path.endswith('.png')):
canvas_print('Please choose image first.')
running = False
return 0
try:
target_img = Image.open(target_img_path)
except:
canvas_print('Can not open the image.')
running = False
return 0
# 屏幕截图
if check_game:
screen = get_image()
if screen is None:
canvas_print('Can not find Draw&Guess.')
running = False
return 0
if screen.size[0] != screen_w or screen.size[1] != screen_h:
canvas_print('Only support fullscreen 1080p.')
running = False
return 0
# 图片过大
tw, th = target_img.size
if tw > th:
resize = screen_w * draw_area / tw
else:
resize = screen_h * draw_area / th
target_img = target_img.resize((int(tw * resize), int(th * resize)), Image.ANTIALIAS)
target_img2 = target_img
tw, th = target_img.size
# 绘画起始点
start_x = int((screen_w * 0.5 * draw_area) - (tw * 0.5) + (screen_w * (1 - draw_area) * 0.5) + point_size * 0.5)
start_y = int((screen_h * 0.5 * draw_area) - (th * 0.5) + (screen_h * (1 - draw_area) * 0.5) + point_size * 0.5)
# 目标图片缩小
tw, th = target_img.size
target_img = target_img.resize((round(tw / point_size), round(th / point_size)), Image.ANTIALIAS)
tw, th = target_img.size
# 减少大量颜色
if reduce_color:
target_img = target_img.convert('P', palette=Image.ADAPTIVE, colors=color_num)
target_img = target_img.convert('RGB', palette=Image.ADAPTIVE, colors=color_num)
target_img_p = target_img.load()
color_dic_temp = {} # 临时字典
def find_color(p):
# 忽略白色
if colors_diff((255, 254, 246), p) < diff_limit:
return None, None
# 扫描临时字典
if p in color_dic_temp:
return color_dic_temp[p]
# 找最接近的颜色
color = closest(p)
# 获取坐标
rx, ry = colors_all_dic[(color[0], color[1], color[2])]
color_dic_temp[p] = (rx, ry)
return rx, ry
# 计算所有取色点
draw_step = {}
for y in range(th): # 先横着画
for x in range(tw):
m, n = find_color(target_img_p[x, y])
if not (m is None):
# 记录操作步骤
if not (m, n) in draw_step:
draw_step[(m, n)] = []
draw_step[(m, n)].append((start_x + point_size * x, start_y + point_size * y))
# 排序, 先画颜色多的点
sort = sorted(draw_step, key=lambda k: len(draw_step[k]), reverse=True)
# 选择最大画笔
if check_game:
pyautogui.click(1307, 73)
# 删除最浅的颜色
if len(sort) == color_num:
sort.remove(color_dic_temp[max(list(color_dic_temp.keys()))])
# 统计需要画的线
for the_color in sort:
last_point = (0, 0)
lines = []
line = {}
for draw_point in draw_step[the_color]:
# 同一行 且 距离够近
if draw_point[1] == last_point[1] and not abs(
draw_point[0] - last_point[0]) > max_dot_pitch_multiple * point_size:
line['end'] = draw_point
else:
if last_point[0] != 0:
lines.append(line)
line = {}
line['start'] = draw_point
last_point = draw_point
# 画线
pyautogui.PAUSE = pause_point
pyautogui.click(the_color[0], the_color[1]) # 色板取色
for the_line in lines:
pyautogui.click(x=the_line['start'][0], y=the_line['start'][1])
if 'end' in the_line:
time = ((((the_line['end'][0] - the_line['start'][0]) ** 2) + (
(the_line['end'][1] - the_line['start'][1]) ** 2)) ** 0.5) / point_size * 0.025
time = max(pause_line, time)
pyautogui.dragTo(the_line['end'][0], the_line['end'][1], time, button='left')
draw2(target_img2)
running = False
window.exit()
# 精细画图
def draw2(target_img2):
point_size = 10 # 画笔大小
reduce_color = True # 减少颜色
color_num = 5 # 颜色数量
max_dot_pitch_multiple = 4 # 最大连线间距倍数
global img_show
global target_img_path
global colors_all
global colors_all_dic
target_img = target_img2
tw, th = target_img.size
# 绘画起始点
start_x = int((screen_w * 0.5 * draw_area) - (tw * 0.5) + (screen_w * (1 - draw_area) * 0.5) + point_size * 0.5)
start_y = int((screen_h * 0.5 * draw_area) - (th * 0.5) + (screen_h * (1 - draw_area) * 0.5) + point_size * 0.5)
# 目标图片缩小
tw, th = target_img.size
target_img = target_img.resize((round(tw / point_size), round(th / point_size)), Image.ANTIALIAS)
tw, th = target_img.size
# 减少大量颜色
if reduce_color:
target_img = target_img.convert('P', palette=Image.ADAPTIVE, colors=color_num)
target_img = target_img.convert('RGB', palette=Image.ADAPTIVE, colors=color_num)
target_img_p = target_img.load()
# 显示预览图
img_show = ImageTk.PhotoImage(target_img)
canvas.create_image(
230.0,
430.0,
image=img_show
)
color_dic_temp = {} # 临时字典
def find_color(p):
# 忽略白色
if colors_diff((255, 254, 246), p) < diff_limit:
return None, None
# 扫描临时字典
if p in color_dic_temp:
return color_dic_temp[p]
# 找最接近的颜色
color = closest(p)
# 获取坐标
rx, ry = colors_all_dic[(color[0], color[1], color[2])]
color_dic_temp[p] = (rx, ry)
return rx, ry
# 计算所有取色点
draw_step = {}
for y in range(th): # 先横着画
for x in range(tw):
m, n = find_color(target_img_p[x, y])
if not (m is None):
# 记录操作步骤
if not (m, n) in draw_step:
draw_step[(m, n)] = []
draw_step[(m, n)].append((start_x + point_size * x, start_y + point_size * y))
# 排序, 先画颜色多的点
sort = sorted(draw_step, key=lambda k: len(draw_step[k]), reverse=True)
# 选择中等画笔
if check_game:
pyautogui.click(1357, 73)
# 删除最浅的颜色
if len(sort) == color_num:
sort.remove(color_dic_temp[max(list(color_dic_temp.keys()))])
# 统计需要画的线
for the_color in sort:
last_point = (0, 0)
lines = []
line = {}
for draw_point in draw_step[the_color]:
# 同一行 且 距离够近
if draw_point[1] == last_point[1] and not abs(
draw_point[0] - last_point[0]) > max_dot_pitch_multiple * point_size:
line['end'] = draw_point
else:
if last_point[0] != 0:
lines.append(line)
line = {}
line['start'] = draw_point
last_point = draw_point
# 画线
pyautogui.PAUSE = pause_point
pyautogui.click(the_color[0], the_color[1]) # 色板取色
for the_line in lines:
pyautogui.click(x=the_line['start'][0], y=the_line['start'][1])
if 'end' in the_line:
time = ((((the_line['end'][0] - the_line['start'][0]) ** 2) + (
(the_line['end'][1] - the_line['start'][1]) ** 2)) ** 0.5) / point_size * 0.025
time = max(pause_line, time)
pyautogui.dragTo(the_line['end'][0], the_line['end'][1], time, button='left')
add_hotkey('f9', start)
###################################
# 主程序
###################################
# 创建程序
window = Tk()
window.geometry("650x537")
window.configure(bg="#FFFFFF")
window.iconbitmap(r'.\app.ico')
window.title('Autodraw')
# 背景图
canvas = Canvas(
window,
bg="#FFFFFF",
height=537,
width=650,
bd=0,
highlightthickness=0,
relief="ridge"
)
canvas.place(x=0, y=0)
image_image = PhotoImage(
file="./assets/bg.png")
canvas.create_image(
325.0,
268.0,
image=image_image
)
def create_text(t):
canvas.create_text(
20.0,
20.0,
anchor="nw",
text=t,
tags=('print',)
)
def canvas_print(t):
canvas.delete('print')
create_text(t)
def button_clicked():
global target_img_path
target_img_path = filedialog.askopenfilename(title="Select An Image",
filetypes=(("jpeg files", "*.jpg"), ("png files", "*.png")))
canvas_print(target_img_path)
# 加载图片按钮
button_image = PhotoImage(
file="./assets/button.png")
button = Button(
image=button_image,
borderwidth=0,
highlightthickness=0,
command=lambda: button_clicked(),
relief="flat"
)
button.place(
x=451.0,
y=124.0,
width=198.0,
height=143.0
)
create_text("Waiting for the image.")
window.resizable(False, False)
window.mainloop()