我有一个用 Python3.5 编写的脚本,带有模块 pyautogui 来记录屏幕上的一个区域,然后将其分成四个象限。我这样做是为了在后续脚本运行时加快搜索速度。为了测试我是否得到象限,我调用了函数 getQuad1 中的屏幕截图命令。当它运行时,它会获得整个屏幕,而不仅仅是我指定的象限。
我不确定我是否搞砸了我的代数,或者是模块在起作用。
import pyautogui
def areaSetup(dimTop, dimBot):
workSpace = getWorkSpace(dimTop , dimBot)
W, H, Hh, Wh = getWaHWorkSpace(workSpace)
quad1 = getQuad1(workSpace,W, H, Hh, Wh)
quad2 = getQuad2(workSpace,W, H, Hh, Wh)
quad3 = getQuad3(workSpace,W, H, Hh, Wh)
quad4 = getQuad4(workSpace,W, H, Hh, Wh)
return workSpace, quad1, quad2, quad3, quad4
def getWorkSpace(browTopLeft, browBotRight):# gets the full area of workspace
top = pyautogui.locateOnScreen( browTopLeft , grayscale=True)
while top is None:
top = pyautogui.locateOnScreen( browTopLeft , grayscale=True)
print (top)
bottom = pyautogui.locateOnScreen( browBotRight , grayscale=True)
while bottom is None:
bottom = pyautogui.locateOnScreen( browBotRight , grayscale=True)
print (bottom)
x1, y1, h1, w1 = top
x2, y2, h2, w2 = bottom
#x2 = x2+w2
#y2 = y2+h2
print ("initial print" , x1, y1, x2, y2)
workSpace = x1, y1, x2, y2
print ("Workspace" , workSpace)
return workSpace
def getWaHWorkSpace(workSpace):
x1, y1, x2, y2 = workSpace #break it into four parts again
W = int(x2 - x1) #get the height
H = int(y2 - y1) #get the width
Hh = int(0.5 * H) #get mid point of height
Wh = int(0.5 * W) #get mid point of width
print ("W and H" , W, H, Hh, Wh)
return W, H, Hh, Wh
def getQuad1(workSpace,W, H, Hh, Wh): #North West Quad
x1, y1, x2, y2 = workSpace #break it into four parts again
q1x1 = int(x1)
q1y1 = int(y1)
q1y2 = int(y1+Hh)
q1x2 = int(x1+Wh)
quad1 = q1x1, q1y1, Wh, Hh
print("quad1", quad1)
pyautogui.screenshot('quad1.png' , region=quad1)#SCREENSHOT TEST
return quad1
通常 areaSetup 与浏览器窗口的左上角和同一窗口的右下角一起运行(这是为了摆弄浏览器表单)。getWorkSpace 运行并获取完整的工作区区域,本质上是浏览器窗口的尺寸。然后有一个名为 getWaHWorkSpace 的脚本,它会计算出每个的宽度、高度和一半以供后续使用。
然后我有一个名为 getQuad1 的函数,它执行指定屏幕左上象限所需的数学运算。还有另外三个函数可以获取其他象限,除了使用的变量之外,它们与 getQuad1 函数相同。
任何有关此的建议或想法将不胜感激!