pyautogui在指定区域时全屏显示

时间:2016-06-09 12:52:55

标签: python screenshot pyautogui

我有一个用Python3.5编写的脚本,模块pyautogui可以记下屏幕上的一个区域,然后将其分成四个象限。我这样做是为了加快搜索,同时后续脚本运行。为了测试我是否获得象限,我在函数getQuad1中调用了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运行并获取完整的workSpace区域,基本上是浏览器窗口的尺寸。然后有一个名为getWaHWorkSpace的脚本,然后计算每个脚本的宽度,高度和一半以供后续使用。

然后我有一个名为getQuad1的函数,它可以完成指定屏幕左上象限所需的数学运算。还有三个函数可以获得其他象限,它们与getQuad1函数相同,除了使用的变量。

对此有任何建议或想法都会非常感激!

1 个答案:

答案 0 :(得分:0)

在做了一些跑步后,我发现这适用于Windows但不适用于Linux(至少Ubuntu 16.04)。我保持代码相同,它在Windows中按预期工作。