/
script.py
277 lines (238 loc) · 8.31 KB
/
script.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from PIL import Image, ImageOps, ImageGrab
import threading, time
def analyze():
screenshot = ImageGrab.grab()
im = screenshot.crop(rect)
im = im.convert("L")
#im.save('test992.png')
W, H = im.size
pix = im.load()
fond = pix[0,0] #pix[abs,ord]
#print(fond)
if fond < 128: #La couleur du fond est foncée
im = ImageOps.invert(im) #On inverse l'image
pix = im.load()
fond = pix[0,0]
#print("Image négativée")
BOXES = []
gauche = 0
droite = 0
bas = 0
haut = H
for w in range(W):
found = False
for h in range(H):
if not pix[w, h] or fond/pix[w, h] > 1.4 : #pixel noir trouvé
found = True
if not gauche:
gauche = w
if w >= droite:
droite = w+1
if h < haut:
haut = h
if h >= bas:
bas = h+1
if not found and gauche: #fin du chiffre
BOXES.append((gauche, haut, droite, bas))
gauche = 0
droite = 0
bas = 0
haut = H
#print(len(BOXES), "chiffres trouvés")
#for i, box in enumerate(BOXES):
# chiffre = im.crop(box)
# chiffre.save(str(i)+'.png')
def vertical(i, chiffre, pix): #Déterminer où sont les pixels sur la ligne verticale
'''
0. En haut et en bas
1. Au dessus du milieu
2. En haut, en dessous du milieu (mais pas en bas) ou au milieu et en bas */***
3. En haut, au milieu et en bas *
4. Au dessus du milieu (mais pas en haut), en dessous du milieu (mais pas en bas)
5. En haut, au milieu et en bas *
6. En haut (ou proche), au milieu et en bas *
7. En haut, milieu (ou proche)
8. En haut, au milieu et en bas *
9. En haut, en dessous du milieu (mais pas en bas) x 2 ou en bas */***
'''
W, H = chiffre.size
milieu_w = int(W/2)
found = False
hauteurs = []
for h in range(H):
if not pix[milieu_w, h] or fond/pix[milieu_w, h] > 1.4:
if not found: #pixel noir trouvé
found = True
hauteurs.append(h)
elif found:
found = False
if len(hauteurs) == 1: #1
#print(i,"is a 1")
return 1
elif len(hauteurs) == 2: #0 4 7
if hauteurs[0] == 0: #0 7
return tiers_sup(i, W, H, milieu_w, pix, (0,7))
else:
#print(i,"is a 4")
return 4
elif len(hauteurs) == 3: #2 3 5 6 8 9
return tiers_sup(i, W, H, milieu_w, pix)
def tiers_sup(i, W, H, milieu_w, pix, d=None): #(*) Distinguer les 0, 7, 2, 3, 5, 6, 8, 9 partiellement
'''
0. A gauche et à droite **
7. A droite **
2. A droite **
3. A droite **
5. A gauche **
6. A gauche **
8. A gauche et à droite **
9. A gauche et à droite **
'''
first_tiers_h = int(H/3)
found = False
largeurs = []
for w in range(W):
if not pix[w, first_tiers_h] or fond/pix[w, first_tiers_h] > 1.4:
if not found: #pixel noir trouvé
found = True
largeurs.append(w)
elif found:
found = False
if d == (0,7):#0 7
if len(largeurs) == 1: #7
return 7
elif len(largeurs) == 2: #0
return 0
if len(largeurs) == 1: #2 3 5 6
if largeurs[0] > milieu_w: #2 3 (A droite)
return tiers_inf(i, W, H, milieu_w, pix, (2,3))
else: #5 6 (A gauche)
return tiers_inf(i, W, H, milieu_w, pix, (5,6))
elif len(largeurs) == 2: #8 9
return tiers_inf(i, W, H, milieu_w, pix, (8,9))
def tiers_inf(i, W, H, milieu_w, pix, d): #(**) Distinguer le 2 du 3 et le 5 du 6 et le 8 du 9
'''
2. Au milieu
3. A droite
5. A droite
6. A gauche et à droite
8. A gauche et à droite
9. A droite
'''
snd_tiers_h = int(H*2/3)
if d == (2,3):
for w in range(W-1,-1,-1): #On commence par la droite
if not pix[w, snd_tiers_h] or fond/pix[w, snd_tiers_h] > 1.4:
if w >= W-(milieu_w/2): #3
#print(i, "is a 3")
return 3
else: #2
#print(i, "is a 2")
return 2
break
elif d == (5,6):
largeurs = []
found = False
for w in range(W):
if not pix[w, snd_tiers_h] or fond/pix[w, snd_tiers_h] > 1.4:
if not found: #pixel noir trouvé
found = True
largeurs.append(w)
elif found:
found = False
if len(largeurs) == 1: #5
#print(i, "is a 5")
return 5
else: #6
#print(i, "is a 6")
return 6
elif d == (8,9):
largeurs = []
found = False
for w in range(W):
if not pix[w, snd_tiers_h] or fond/pix[w, snd_tiers_h] > 1.4:
if not found: #pixel noir trouvé
found = True
largeurs.append(w)
elif found:
found = False
if len(largeurs) == 1: #9
#print(i, "is a 9")
return 9
else: #8
#print(i, "is a 8")
return 8
result = []
for i, box in enumerate(BOXES):
chiffre = im.crop(box)
pix = chiffre.load()
result.append(vertical(i, chiffre, pix))
print('RESULTAT :', ''.join(str(_) for _ in result))
x0, y0, x1, y1 = 0, 0, 0, 0
def start(event):
global FRAME, x0, y0, top#, count, st
x0, y0 = event.x, event.y
#count, st = 0, 0
try:
FRAME.destroy()
except:
pass
try:
top.destroy()
except:
pass
FRAME = tk.Frame(root, width = 20, height = 20, bg = 'white')
root.event_generate("<Motion>", warp=True, x=x0+20, y=y0+20)
FRAME.place(x=x0,y=y0)
def draw(event):
global FRAME, label_started
x, y = event.x, event.y
FRAME.configure(width=abs(x0-x), height=abs(y0-y))
def release(event):
global FRAME, x1, y1, top, button
x1, y1 = event.x, event.y
top = tk.Toplevel(root)
top.attributes('-alpha', 1)
top.wm_attributes("-topmost", True)
top.overrideredirect(True)
button = ttk.Button(top, text='START')
button.bind('<ButtonRelease-1>', button_release)
button.pack()
top.geometry(f"{button.winfo_reqwidth()}x{button.winfo_reqheight()}+{x1+5}+{y1}")
#FRAME.destroy()
def leave(event):
root.wm_state('iconic')
#st = 0
def button_release(event):
global rect#, st
rect = (x0, y0, x1, y1)
top.destroy()
FRAME.destroy()
root.wm_state('iconic')
#st = time.time()
threading.Thread(target=boucle, daemon=True).start()
#count = 0
def boucle():
#global count
while True:
analyze()
#count+=1
#if count == 30:
# print(time.time()-st, f'secondes pour faire {count} recherches')
# return
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.geometry("1920x1080+0+0")
#root.lift()
root.wm_attributes("-topmost", True)
root.attributes('-fullscreen', True)
root.configure(background='blue2')
root.attributes("-transparentcolor", "white")
root.attributes('-alpha', 0.30)
root.config(cursor="cross")
root.bind('<ButtonPress-1>', start)
root.bind('<B1-Motion>', draw)
root.bind('<ButtonRelease-1>', release)
root.bind('<Escape>', leave)
root.mainloop()