截圖函數
PyAutoGUI可以截取屏幕截圖,將其保存到文件中,並在屏幕中查找圖像。如果您有一個小圖像,例如需要單擊並希望在屏幕上找到它的按鈕,這將非常有用。這些功能由PyScreeze模塊提供,該模塊與PyAutoGUI一起安裝。
屏幕截圖功能需要Pillow模塊。OS X使用操作系統screencapture
附帶的命令。Linux使用該scrot
命令,可以通過運行來安裝。sudo apt-get install scrot
截圖()函數
調用screenshot()
將返回Image對象(有關詳細信息,請參閱Pillow或PIL模塊文檔)。傳遞文件名字符串會將屏幕截圖保存到文件中,並將其作為Image對象返回。
>>> import pyautogui >>> im1 = pyautogui.screenshot() >>> im2 = pyautogui.screenshot('my_screenshot.png')
在1920 x 1080的屏幕上,該screenshot()
功能大約需要100毫秒 - 它並不快,但速度並不慢。
region
如果您不想要整個屏幕的屏幕截圖,還有一個可選的關鍵字參數。您可以傳遞區域左側,頂部,寬度和高度的四個整數元組來捕獲:
>>> import pyautogui >>> im = pyautogui.screenshot(region=(0,0, 300, 400))
定位函數
注意:從版本0.9.41開始,如果locate函數找不到提供的圖像,它們將引發ImageNotFoundException
而不是返回None
。
如果您有圖像文件,可以在屏幕上直觀地找到某些內容。例如,假設計算器應用程序正在您的計算機上運行,看起來像這樣:

如果您不知道計算器按鈕的確切屏幕坐標,則無法調用moveTo()
和click()
函數。每次啟動計算器時,計算器都會出現略微不同的位置,導致您每次都重新找到坐標。但是,如果您有按鈕的圖像,例如7按鈕的圖像:

...你可以調用locateOnScreen('calc7key.png')
函數來獲取屏幕坐標。返回值是一個4整數元組:(左,頂,寬,高)。可以傳遞此元組center()
以獲得該區域中心的X和Y坐標。如果在屏幕上找不到圖像,則locateOnScreen()
加注ImageNotFoundException
。
>>> import pyautogui >>> button7location = pyautogui.locateOnScreen('calc7key.png') >>> button7location Box(left=1416, top=562, width=50, height=41) >>> button7location[0] 1416 >>> button7location.left 1416 >>> button7point = pyautogui.center(button7location) >>> button7point Point(x=1441, y=582) >>> button7point[0] 1441 >>> button7point.x 1441 >>> button7x, button7y = button7point >>> pyautogui.click(button7x, button7y) # clicks the center of where the 7 button was found >>> pyautogui.click('calc7key.png') # a shortcut version to click on the center of where the 7 button was found
可選confidence
關鍵字參數指定函數在屏幕上定位圖像的准確性。如果函數由於可忽略的像素差異而無法定位圖像,這將非常有用:
>>> import pyautogui >>> button7location = pyautogui.locateOnScreen('calc7key.png', confidence=0.9) >>> button7location Box(left=1416, top=562, width=50, height=41)
該locateCenterOnScreen()
功能結合locateOnScreen()
和center()
:
>>> import pyautogui >>> x, y = pyautogui.locateCenterOnScreen('calc7key.png') >>> pyautogui.click(x, y)
在1920 x 1080屏幕上,定位功能調用大約需要1或2秒。這對於動作視頻游戲來說可能太慢,但適用於大多數目的和應用程序。
有幾個“定位”功能。他們都開始查看屏幕的左上角(或圖像)並向右看,然后向下看。參數可以是a
locateOnScreen(image, grayscale=False)
- 返回image
屏幕上第一個找到的實例的(左,頂部,寬度,高度)坐標。ImageNotFoundException
如果在屏幕上找不到則會引發。locateCenterOnScreen(image, grayscale=False)
- 返回image
屏幕上第一個找到的實例中心的(x,y)坐標。ImageNotFoundException
如果在屏幕上找不到則會引發。locateAllOnScreen(image, grayscale=False)
- 返回一個生成器,該生成器生成(左,頂部,寬度,高度)元組,用於在屏幕上找到圖像的位置。locate(needleImage, haystackImage, grayscale=False)
-返回(左,上,寬度,高度)的第一坐標發現的實例needleImage
在haystackImage
。ImageNotFoundException
如果在屏幕上找不到則會引發。locateAll(needleImage, haystackImage, grayscale=False)
- 返回一個生成器(生成(左,頂部,寬度,高度)元組的位置needleImage
)haystackImage
。
“locate all”函數可用於for循環或傳遞給list()
:
>>> import pyautogui >>> for pos in pyautogui.locateAllOnScreen('someButton.png') ... print(pos) ... (1101, 252, 50, 50) (59, 481, 50, 50) (1395, 640, 50, 50) (1838, 676, 50, 50) >>> list(pyautogui.locateAllOnScreen('someButton.png')) [(1101, 252, 50, 50), (59, 481, 50, 50), (1395, 640, 50, 50), (1838, 676, 50, 50)]
這些“定位”功能相當昂貴; 他們可以花一整秒鍾來跑步。加速它們的最好方法是傳遞一個region
參數(4個整數元組(左,頂部,寬度,高度))來僅搜索屏幕的較小區域而不是全屏:
>>> import pyautogui >>> pyautogui.locateOnScreen('someButton.png', region=(0,0, 300, 400))
灰度匹配
或者,您可以傳遞grayscale=True
給locate函數以提供輕微的加速(大約30%-ish)。這會使圖像和屏幕截圖中的顏色去飽和,從而加快定位速度,但可能導致誤判。
>>> import pyautogui >>> button7location = pyautogui.locateOnScreen('calc7key.png', grayscale=True) >>> button7location (1416, 562, 50, 41)
像素匹配
要獲取屏幕截圖中像素的RGB顏色,請使用Image對象的getpixel()
方法:
>>> import pyautogui >>> im = pyautogui.screenshot() >>> im.getpixel((100, 200)) (130, 135, 144)
或者作為單個函數,調用pixel()
PyAutoGUI函數,它是以前調用的包裝器:
>>> import pyautogui >>> pix = pyautogui.pixel(100, 200) >>> pix RGB(red=130, green=135, blue=144) >>> pix[0] 130 >>> pix.red 130
如果您只需要驗證單個像素與給定像素匹配,請調用該pixelMatchesColor()
函數,向其傳遞X坐標,Y坐標和它所代表顏色的RGB元組:
>>> import pyautogui >>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144)) True >>> pyautogui.pixelMatchesColor(100, 200, (0, 0, 0)) False
可選tolerance
關鍵字參數指定在匹配時每個紅色,綠色和藍色值可以變化的程度:
>>> import pyautogui >>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144)) True >>> pyautogui.pixelMatchesColor(100, 200, (140, 125, 134)) False >>> pyautogui.pixelMatchesColor(100, 200, (140, 125, 134), tolerance=10) True
來源:https://pyautogui.readthedocs.io/en/latest/screenshot.html#