我有一个用Python3.5编写的脚本,其中包含pyautogui模块,用于记录屏幕上的一个区域,然后将其分成四个象限。我这样做是为了在后续脚本运行时加快搜索速度。为了测试我是否得到了象限,我调用了getquadran1函数中的screenshot命令。当它运行时,它会得到整个屏幕,而不仅仅是我指定的象限。在
我不确定是我弄错了代数,还是模块出了问题。在
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函数相同,只不过使用了变量。在
如果您对此有任何建议或想法,我们将不胜感激!在
在运行了一段时间后,我发现这在Windows下可以工作,但在Linux中不行(至少是ubuntu16.04)。我保持了相同的代码,它在Windows中可以正常工作。在
相关问题 更多 >
编程相关推荐