-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrn.py
180 lines (119 loc) · 4.39 KB
/
scrn.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
from heads import *
import g
exec(open('impo.py').read())
def genCells(X, Y, W, H, rows, cols) :
cells = {}
cell_width = W // cols
cell_height = H // rows
n = 0
for i in range(rows):
for j in range(cols):
letter = g.letterList [n]
cells[letter] = {
'x': j*cell_width ,
'y': i*cell_height ,
'w': cell_width,
'h': cell_height,
}
n += 1
return cells
def destroyWindow () :
print("destroyWindow()")
hideWindow()
g.wdapp.pub_quit.emit()
g.wdapp = None
def hideWindow() :
if g.wdapp:
g.wdapp.pub_hide.emit()
else:
print('WARN: hideWindow() called but no g.wdapp')
g.showingScreen = False
g.keypList = []
def showWindow() :
print('showWindow()')
if g.wdapp:
g.wdapp.pub_show.emit()
else:
print('create thread for QtApplication')
qtthread = Thread(target=createWindow, args=(g.scrX, g.scrY, g.scrW, g.scrH) )
qtthread.start()
def createWindow(x, y, w,h):
if g.wdapp:
print('ERROR: createWindow() called but g.wdapp is not None')
return
g.wdapp = WdApp([x, y, w, h])
g.wdapp.pub_show.connect(g.wdapp.show)
g.wdapp.pub_hide.connect(g.wdapp.hide)
g.wdapp.pub_refresh.connect(g.wdapp.slot_refresh)
g.wdapp.pub_quit.connect(g.wdapp.slot_quit)
g.wdapp.pub_show.emit()
g.wdapp.exec_()
print('after app.exec_()')
def screen_away() :
g.showingScreen = False
hideWindow()
resetKeyPrsd()
resetRegions()
def screen_do(scrType) :
g.showingScreen = scrType
resetKeyPrsd()
if scrType == 'keys':
imgScrn = take_screenshot(g.scrX+g.curCellX, g.scrY+g.curCellY, g.curCellW, g.curCellH)
processScreenshot(imgScrn)
showWindow()
def processScreenshot(imgScrn):
imgOrig = convertQImageToMat(imgScrn)
# imgOrig = np.asarray(imgScrn)
imgGray = cv2.cvtColor(imgOrig, cv2.COLOR_BGR2GRAY)
# C>0 以 白底黑字 方式做二值 输出是 白底黑字+反色的被描边
imgThrW = cv2.adaptiveThreshold(imgGray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY , 3 , 3)
# C<=0 以 黑底白字 方式做二值 输出是 黑底白字+反色的被描边
# imgThrB = cv2.adaptiveThreshold(imgGray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY , 3 , -3)
# 输入 是 白底黑字(w) or 黑底白字(b)
worb_input = 'w' # w or b
if worb_input == 'w':
imgUsedForDlting = invImg ( imgThrW )
else :
imgUsedForDlting = imgThrB
# cv.dilate 默认情况下应 输入 黑底白字 的图 输出也是 黑底白字
imgDlt = cv.dilate( imgUsedForDlting, cv2.getStructuringElement(cv2.MORPH_RECT, ksize=(3,1)) )
# 默认情况closing操作应输入 黑底白字 的图 输出也是 黑底白字
imgCls = cv2.morphologyEx(imgDlt, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, ksize=(3,1)) )
imgMser, g.regions = mserImg(invImg(imgCls) , imgOrig)
updateRegions(g.regions)
def take_screenshot(x, y, w, h):
sc = None
needCreate = False
if g.wdapp:
sc = g.wdapp
else:
needCreate = True
sc = QApplication([])
# QScreen.grabWindow( sc.primaryScreen(), QApplication.desktop().winId() ) .save(filename, 'png')
imgScrn = QScreen.grabWindow( sc.primaryScreen(), QApplication.desktop().winId() , x, y, w, h).toImage()
if needCreate:
sc.quit()
return imgScrn
def get_desktop_size() :
tmpapp = QGuiApplication([])
# screens = QGuiApplication.screens()
# total_geometry = QRect()
# for s in screens:
# total_geometry = total_geometry.united(s.geometry())
# print(total_geometry.x(), total_geometry.y(), total_geometry.width(), total_geometry.height())
#
# w = total_geometry.width()
# h = total_geometry.height()
# # 获取屏幕大小并设置窗口大小
screen = QGuiApplication.primaryScreen()
if screen is not None:
rect = screen.availableGeometry()
print(rect)
else:
print('ERROR: failed to get primary screen')
x = rect.x()
y = rect.y()
w = rect.width()
h = rect.height()
tmpapp.quit()
return [x, y, w, h]