-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini.py
133 lines (105 loc) · 3.05 KB
/
mini.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
"""控制天天象棋的微信小程序电脑端窗口
get_pgn 用于导出 pgn 文件
"""
import os
import time
from PIL import Image
import cv2 as cv
import numpy as np
import pyautogui
from pygetwindow import getWindowsWithTitle
# 小程序登录器路径
WECHATAPPLAUNCHERPATH = "D:\\Program Files (x86)\\Tencent\\WeChat\\WechatAppLauncher.exe"
class Mini(object):
"""模拟微信小程序的行为"""
def __init__(self,title,launch_appid = None):
self.title = title
try:
self.window = getWindowsWithTitle(self.title)[0]
except:
self.open(launch_appid)
while True:
try:
self.window = getWindowsWithTitle(self.title)[0]
break
except:
continue
self.minimize = self.window.minimize
self.restore = self.window.restore
self.close = self.window.close
self.move = self.window.moveTo
self.resize = self.window.resizeTo
self.activate = self.window.activate
self.original_width = self.window.width
self.original_height = self.window.height
@property
def width(self):
return self.window.width
@property
def height(self):
return self.window.height
@property
def box(self):
return self.window.box
def open(self,launch_appid):
print('正在启动' + self.title)
cmd = '"%s" -launch_appid=%s' % (WECHATAPPLAUNCHERPATH,launch_appid)
os.system(cmd)
print('启动成功' + self.title)
self.is_open = True
def close(self):
self.window.close()
def click(self,x,y):
pyautogui.click(x,y)
def pic2move(image):
"""通过找○得到步骤"""
board_box =(5,160,320,505)
vlines = "ABCDEFGHI"
# 模板找图找到落点---------------------------------
board_image = image.crop(board_box)
to = pyautogui.locate('static/template/to.png',board_image,confidence=0.8)
tx, ty = (to.left-16)//34, to.top//34
TO = vlines[tx], str(9-ty)
# 找圆法找到落点---------------------------------
board_image = board_image.convert('L')
binary = board_image.point(lambda i: 255 if i>225 else 0)
binary = np.asarray(binary)
# 调参
board_circle = cv.HoughCircles(binary,cv.HOUGH_GRADIENT,1,10,
param1=50,param2=8,minRadius=2,maxRadius=5)
# print(board_circle)
# cv.imshow('',binary)
circles = np.uint32(np.around(board_circle))
cs = []
for index,i in enumerate(circles[0,:]):
cv.circle(binary,(i[0],i[1]),5*i[2],255,1)
cs.append((i[2],i[0]//34,i[1]//34))
assert len(cs) == 1
FROM = vlines[cs[0][1]],str(9-cs[0][2])
return ''.join(FROM)+'-'+''.join(TO)
def get_pgn(fp):
b = []
board_box =(4,158,319,506)
next_ = (226, 590)
checkpoint = (230,590)
a = getWindowsWithTitle('天天象棋')[0]
a.moveTo(0,0)
s = pyautogui.screenshot(region=a.box)
while True:
if s.getpixel(checkpoint) == (209,206,203):
pyautogui.click(next_)
time.sleep(2) # 为了防止将军动画的影响,2s走一步
else:
break
s = pyautogui.screenshot(region=a.box)
b.append(pic2move(s))
with open(fp,'w') as f:
for index, step in enumerate(b):
if index%2==0:
f.write(str(index//2+1)+'. ')
f.write(step+' ')
if index%2==1:
f.write('\n')
return b
if __name__ == '__main__':
get_pgn('static/pgn/2.pgn')