PyAutoGUI can take screenshots, save them to files, and locate images within the screen. This is useful if you have a small
image of, say, a button that needs to be clicked and want to locate it on the screen. These features are provided by the PyScreeze module, which is installed with PyAutoGUI. Screenshot functionality requires the Pillow module. OS X uses the Calling On a 1920 x 1080 screen, the There is also an optional >>> import pyautogui >>> im = pyautogui.screenshot(region=(0,0, 300, 400)) The Locate Functions¶NOTE: As of version 0.9.41, if the
locate functions can’t find the provided image, they’ll raise You can visually locate something on the screen if you have an image file of it. For example, say the calculator app was running on your computer and looked like this: You can’t call the … you can call the >>> 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 The optional >>> import pyautogui >>> button7location = pyautogui.locateOnScreen('calc7key.png', confidence=0.9) >>> button7location Box(left=1416, top=562, width=50, height=41) Note: You need to have
OpenCV installed for the The >>> import pyautogui >>> x, y = pyautogui.locateCenterOnScreen('calc7key.png') >>> pyautogui.click(x, y) On a 1920 x 1080 screen, the locate function calls take about 1 or 2 seconds. This may be too slow for action video games, but works for most purposes and applications. There are several “locate” functions. They all start looking at the top-left corner of the screen (or image) and look to the right and then down. The arguments can either be a
The “locate all” functions can be used in for loops or passed to >>> 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)] These “locate” functions are fairly expensive; they can take a full second to run. The
best way to speed them up is to pass a >>> import pyautogui >>> pyautogui.locateOnScreen('someButton.png', region=(0,0, 300, 400)) Grayscale Matching¶Optionally, you can pass >>> import pyautogui >>> button7location = pyautogui.locateOnScreen('calc7key.png', grayscale=True) >>> button7location (1416, 562, 50, 41) Pixel Matching¶To obtain the RGB color of a pixel in a screenshot, use the Image object’s >>> import pyautogui >>> im = pyautogui.screenshot() >>> im.getpixel((100, 200)) (130, 135, 144) Or as a single
function, call the >>> import pyautogui >>> pix = pyautogui.pixel(100, 200) >>> pix RGB(red=130, green=135, blue=144) >>> pix[0] 130 >>> pix.red 130 If you just need to verify that a single pixel matches a given pixel, call the >>> import pyautogui >>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144)) True >>> pyautogui.pixelMatchesColor(100, 200, (0, 0, 0)) False The optional >>> 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 |