-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboardGrab.py
253 lines (203 loc) · 6.29 KB
/
boardGrab.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
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
from sys import argv
from PIL import Image
try:
from PIL import ImageGrab
except:
print("Can't import ImageGrab")
try:
import win32api, win32con
except Exception, e:
print("Can't import win32api, win32con")
import time
wwfTopLeft = Image.open("rsrc/wwfTopLeft.png")
offset = (90,75)
def drag(x0, y0, x1, y1):
win32api.SetCursorPos((x0,y0))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0)
time.sleep(0.5)
win32api.SetCursorPos((x1,y1))
time.sleep(0.25)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0)
time.sleep(0.25)
def findSubimage(needle, haystack):
haySize = haystack.size
needleSize = needle.size
maxX = haySize[0] - needleSize[0]
maxY = haySize[1] - needleSize[1]
#TODO could be faster using needle.load() and haystack.load()
for i in range(maxX):
for j in range(maxY):
okay = True
for dx in range(needleSize[0]):
for dy in range(needleSize[1]):
if needle.getpixel((dx, dy)) != haystack.getpixel((i+dx, j+dy)):
okay = False
break
if not okay:
break
if okay:
print((i, j))
return (i, j)
print('guess!!!')
haystack.save("hay.png")
return offset
def getWWFImage():
screen = ImageGrab.grab()
topleft = wwfTopLeft
coords = findSubimage(topleft, screen)
global offset
offset = coords
if not coords:
print("Fail")
return None
boardL = 224 + coords[0] #offsets from wwfTopLeft.png to boardTL
boardT = 49 + coords[1]
boardBox = (boardL , boardT, boardL + 525, boardT + 575)
board = screen.crop(boardBox)
if __name__ == '__main__':
board.save('board.png')
return board
def bwOfImage(im):
colors = [255]*256*3
colors[0] = 0
colors[1] = 0
colors[256] = 0
colors[257] = 0
colors[512] = 0
colors[513] = 0
colors[0+255] = 0
colors[256+255] = 0
colors[512+255] = 0
return im.point(colors).convert("1")
def getLetterOnRack(im, i):
s = 38
left = 130
top = 533
tileImage = bwOfImage(im.crop((left+i*s, top, left+i*s+s, top+s)))
if __name__ == "__main__" and argv[1] == "RACK":
tileImage.save("rsrc/bwLarge/%s.png" % argv[2][i])
alphabet = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
bestDistance = s*s*s
bestLetter = None
for letter in alphabet:
letterImage = Image.open("rsrc/bwLarge/%s.png" % letter).convert("1")
distance = imageDistance(letterImage, tileImage)
if distance < bestDistance:
bestDistance = distance
bestLetter = letter
if bestLetter == '_' or bestLetter == '@' or bestLetter == '3':
bestLetter = '_'
return bestLetter
def imageDistance(im1, im2): #should be same size and square
distance = 0
s = im1.size[0]
for x in range(s):
for y in range(s):
if im1.getpixel((x, y)) != im2.getpixel((x, y)):
distance+=1
return distance
def getLetterAt(board, x, y):
s = 35
tileImage = board.crop((x*s, y*s, x*s+s, y*s+s))
tileImage = bwOfImage(tileImage)
if __name__ == "__main__" and argv[1] != "GO" and argv[1] != "FIND":
tileImage.save("rsrc/bw/%s.png" % argv[3])
elif __name__ == "__main__" and argv[1] == "FIND":
tileImage.save("_temp.png")
alphabet = "_@3ABCDEFGHIJKLMNOPQRSTUVWXYZ"
bestDistance = s*s*s
bestLetter = None
for letter in alphabet:
letterImage = Image.open("rsrc/bw/%s.png" % letter).convert("1")
distance = imageDistance(letterImage, tileImage)
if distance < bestDistance:
bestDistance = distance
bestLetter = letter
if bestLetter == '_' or bestLetter == '@' or bestLetter == '3':
bestLetter = None
return bestLetter
def getWWFData():
boardIm = getWWFImage()
board = {}
for row in range(15):
board[row] = {}
for col in range(15):
board[row][col] = getLetterAt(boardIm, col, row)
if __name__ == '__main__':
if board[row][col]:
print(board[row][col],)
else:
print(' ',)
if __name__ == '__main__':
print()
print("Board grabbed!")
rack = {}
for i in range(7):
rack[i] = getLetterOnRack(boardIm, i)
return (board, rack)
def moveFromRackToBoard(i, x, y):
x0 = offset[0] + 38 * i + 130 + 224 + 19
y0 = offset[1] + 533 + 49 + 19
x1 = offset[0] + 35 * x + 224 + 17
y1 = offset[1] + 35 * y + 49 + 17
drag(x0, y0, x1, y1)
def clickAt(x0, y0):
win32api.SetCursorPos((x0,y0))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0)
time.sleep(0.5)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0)
def shuffleOrRecall():
x0 = offset[0] + 38 * 8 + 130 + 224 + 40
y0 = offset[1] + 533 + 49 + 19
clickAt(x0, y0)
def clickSendRequest():
screen = ImageGrab.grab()
im = wwfTopLeft
def finalizeMove():
x0 = offset[0] + 38 * 8 + 130 + 224
y0 = offset[1] + 533 + 49 + 19
clickAt(x0, y0)
time.sleep(2)
x1 = offset[0] + 35 * (9) + 224 + 17
y1 = offset[1] + 35 * (7) + 49 + 17
clickAt(x1, y1)
clickSendRequest()
def playMove(board, rack, result):
dx = 0
dy = 0
if result[2][2]:
dx = 1
else:
dy = 1
x = result[2][0]
y = result[2][1]
for c in result[1]:
if not board[x][y]['letter']:
board[x][y]['letter'] = c
i = rack.index(c)
moveFromRackToBoard(i, y, x)
rack[i] = ' '
x+=dx
y+=dy
finalizeMove()
def main():
if argv[1] == "RACK":
board = getWWFImage()
for i in range(7):
print(getLetterOnRack(board, i),)
elif argv[1] == "DRAG":
drag(int(argv[2]), int(argv[3]), int(argv[4]), int(argv[5]))
elif argv[1] == "FIND":
board = getWWFImage()
getLetterAt(board, int(argv[2]), int(argv[3]))
elif argv[1] == "GO":
getWWFData()
im = getWWFImage()
for i in range(7):
print(getLetterOnRack(im, i),)
print()
else:
board = getWWFImage()
getLetterAt(board, int(argv[1]), int(argv[2]))
if __name__ == "__main__":
main()