Debian Codegraph @

docs/screenshot.rst

raw
1 .. default-role:: code
2
3 ====================
4 Screenshot Functions
5 ====================
6
7 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.
8
9 Screenshot functionality requires the Pillow module. OS X uses the `screencapture` command, which comes with the operating system. Linux uses the `scrot` command, which can be installed by running `sudo apt-get install scrot`.
10
11 The screenshot() Function
12 =========================
13
14 Calling `screenshot()` will return an Image object (see the Pillow or PIL module documentation for details). Passing a string of a filename will save the screenshot to a file as well as return it as an Image object.
15
16 .. code:: python
17
18 >>> import pyautogui
19 >>> im1 = pyautogui.screenshot()
20 >>> im2 = pyautogui.screenshot('my_screenshot.png')
21
22 On a 1920 x 1080 screen, the `screenshot()` function takes roughly 100 milliseconds - it's not fast but it's not slow.
23
24 There is also an optional `region` keyword argument, if you do not want a screenshot of the entire screen. You can pass a four-integer tuple of the left, top, width, and height of the region to capture:
25
26 >>> import pyautogui
27 >>> im = pyautogui.screenshot(region=(0,0, 300, 400))
28
29 The Locate Functions
30 ====================
31
32 NOTE: As of version 0.9.41, if the locate functions can't find the provided image, they'll raise `ImageNotFoundException` instead of returning `None`.
33
34 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:
35
36 .. image:: calculator.png
37
38 You can't call the `moveTo()` and `click()` functions if you don't know the exact screen coordinates of where the calculator buttons are. The calculator can appear in a slightly different place each time it is launched, causing you to re-find the coordinates each time. However, if you have an image of the button, such as the image of the 7 button:
39
40 .. image:: calc7key.png
41
42 . . . you can call the `locateOnScreen('calc7key.png')` function to get the screen coordinates. The return value is a 4-integer tuple: (left, top, width, height). This tuple can be passed to `center()` to get the X and Y coordinates at the center of this region. If the image can't be found on the screen, `locateOnScreen()` raises `ImageNotFoundException`.
43
44 >>> import pyautogui
45 >>> button7location = pyautogui.locateOnScreen('calc7key.png')
46 >>> button7location
47 Box(left=1416, top=562, width=50, height=41)
48 >>> button7location[0]
49 1416
50 >>> button7location.left
51 1416
52 >>> button7point = pyautogui.center(button7location)
53 >>> button7point
54 Point(x=1441, y=582)
55 >>> button7point[0]
56 1441
57 >>> button7point.x
58 1441
59 >>> button7x, button7y = button7point
60 >>> pyautogui.click(button7x, button7y) # clicks the center of where the 7 button was found
61 >>> pyautogui.click('calc7key.png') # a shortcut version to click on the center of where the 7 button was found
62
63 The optional `confidence` keyword argument specifies the accuracy with which the function should locate the image on screen. This is helpful in case the function is not able to locate an image due to negligible pixel differences:
64
65 >>> import pyautogui
66 >>> button7location = pyautogui.locateOnScreen('calc7key.png', confidence=0.9)
67 >>> button7location
68 Box(left=1416, top=562, width=50, height=41)
69
70 **Note**: You need to have `OpenCV <https://pypi.org/project/opencv-python/>`_ installed for the `confidence` keyword to work.
71
72 The `locateCenterOnScreen()` function combines `locateOnScreen()` and `center()`:
73
74 >>> import pyautogui
75 >>> x, y = pyautogui.locateCenterOnScreen('calc7key.png')
76 >>> pyautogui.click(x, y)
77
78 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.
79
80 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
81
82 - `locateOnScreen(image, grayscale=False)` - Returns (left, top, width, height) coordinate of first found instance of the `image` on the screen. Raises `ImageNotFoundException` if not found on the screen.
83
84 - `locateCenterOnScreen(image, grayscale=False)` - Returns (x, y) coordinates of the center of the first found instance of the `image` on the screen. Raises `ImageNotFoundException` if not found on the screen.
85
86 - `locateAllOnScreen(image, grayscale=False)` - Returns a generator that yields (left, top, width, height) tuples for where the image is found on the screen.
87
88 - `locate(needleImage, haystackImage, grayscale=False)` - Returns (left, top, width, height) coordinate of first found instance of `needleImage` in `haystackImage`. Raises `ImageNotFoundException` if not found on the screen.
89
90 - `locateAll(needleImage, haystackImage, grayscale=False)` - Returns a generator that yields (left, top, width, height) tuples for where `needleImage` is found in `haystackImage`.
91
92 The "locate all" functions can be used in for loops or passed to `list()`:
93
94 >>> import pyautogui
95 >>> for pos in pyautogui.locateAllOnScreen('someButton.png')
96 ... print(pos)
97 ...
98 (1101, 252, 50, 50)
99 (59, 481, 50, 50)
100 (1395, 640, 50, 50)
101 (1838, 676, 50, 50)
102 >>> list(pyautogui.locateAllOnScreen('someButton.png'))
103 [(1101, 252, 50, 50), (59, 481, 50, 50), (1395, 640, 50, 50), (1838, 676, 50, 50)]
104
105 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 `region` argument (a 4-integer tuple of (left, top, width, height)) to only search a smaller region of the screen instead of the full screen:
106
107 >>> import pyautogui
108 >>> pyautogui.locateOnScreen('someButton.png', region=(0,0, 300, 400))
109
110 Grayscale Matching
111 ------------------
112
113 Optionally, you can pass `grayscale=True` to the locate functions to give a slight speedup (about 30%-ish). This desaturates the color from the images and screenshots, speeding up the locating but potentially causing false-positive matches.
114
115 >>> import pyautogui
116 >>> button7location = pyautogui.locateOnScreen('calc7key.png', grayscale=True)
117 >>> button7location
118 (1416, 562, 50, 41)
119
120 Pixel Matching
121 --------------
122
123 To obtain the RGB color of a pixel in a screenshot, use the Image object's `getpixel()` method:
124
125 >>> import pyautogui
126 >>> im = pyautogui.screenshot()
127 >>> im.getpixel((100, 200))
128 (130, 135, 144)
129
130 Or as a single function, call the `pixel()` PyAutoGUI function, which is a wrapper for the previous calls:
131
132 >>> import pyautogui
133 >>> pix = pyautogui.pixel(100, 200)
134 >>> pix
135 RGB(red=130, green=135, blue=144)
136 >>> pix[0]
137 130
138 >>> pix.red
139 130
140
141 If you just need to verify that a single pixel matches a given pixel, call the `pixelMatchesColor()` function, passing it the X coordinate, Y coordinate, and RGB tuple of the color it represents:
142
143 >>> import pyautogui
144 >>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144))
145 True
146 >>> pyautogui.pixelMatchesColor(100, 200, (0, 0, 0))
147 False
148
149 The optional `tolerance` keyword argument specifies how much each of the red, green, and blue values can vary while still matching:
150
151 >>> import pyautogui
152 >>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144))
153 True
154 >>> pyautogui.pixelMatchesColor(100, 200, (140, 125, 134))
155 False
156 >>> pyautogui.pixelMatchesColor(100, 200, (140, 125, 134), tolerance=10)
157 True