0
# Screen Capture
1
2
Bitmap operations for capturing screenshots of controls or screen areas, pixel color analysis, and image-based verification in automation scripts. This capability enables visual testing and verification workflows.
3
4
## Capabilities
5
6
### Bitmap Class
7
8
The main class for image capture and manipulation operations.
9
10
```python { .api }
11
class Bitmap:
12
"""Class for image capture and manipulation."""
13
14
@staticmethod
15
def FromControl(control: Control) -> Bitmap:
16
"""
17
Capture a control as a bitmap image.
18
19
Args:
20
control: Control to capture
21
22
Returns:
23
Bitmap: Bitmap image of the control
24
"""
25
26
@staticmethod
27
def FromFile(filename: str) -> Bitmap:
28
"""
29
Load a bitmap from an image file.
30
31
Args:
32
filename: Path to image file
33
34
Returns:
35
Bitmap: Loaded bitmap image
36
"""
37
38
def ToFile(self, filename: str) -> None:
39
"""
40
Save the bitmap to an image file.
41
42
Args:
43
filename: Output file path (supports .bmp, .png, .jpg formats)
44
"""
45
46
def GetPixelColor(self, x: int, y: int) -> int:
47
"""
48
Get the color of a pixel at the specified coordinates.
49
50
Args:
51
x: X coordinate within the bitmap
52
y: Y coordinate within the bitmap
53
54
Returns:
55
int: Color value as an integer (0xAARRGGBB format)
56
"""
57
58
def SetPixelColor(self, x: int, y: int, color: int) -> None:
59
"""
60
Set the color of a pixel at the specified coordinates.
61
62
Args:
63
x: X coordinate within the bitmap
64
y: Y coordinate within the bitmap
65
color: Color value as an integer (0xAARRGGBB format)
66
"""
67
68
@property
69
def Width(self) -> int:
70
"""Width of the bitmap in pixels."""
71
72
@property
73
def Height(self) -> int:
74
"""Height of the bitmap in pixels."""
75
```
76
77
### Control Screen Capture Methods
78
79
Methods available on Control objects for capturing screenshots.
80
81
```python { .api }
82
class Control:
83
def CaptureToImage(self, filename: str) -> None:
84
"""
85
Capture this control to an image file.
86
87
Args:
88
filename: Output file path for the screenshot
89
"""
90
91
```
92
93
### System-Level Screen Capture
94
95
Pixel color information from the Win32API.
96
97
```python { .api }
98
# Available via Win32API class
99
from uiautomation import Win32API
100
101
def GetPixelColor(x: int, y: int) -> int:
102
"""
103
Get the color of a pixel at screen coordinates.
104
105
Args:
106
x: Screen X coordinate
107
y: Screen Y coordinate
108
109
Returns:
110
int: Color value as an integer (0xAARRGGBB format)
111
"""
112
```
113
114
115
## Usage Examples
116
117
### Basic Screen Capture
118
119
```python
120
import uiautomation
121
122
# Capture specific control (main way to capture images)
123
window = uiautomation.WindowControl(Name='Calculator')
124
if window.Exists():
125
window.CaptureToImage("calculator_screenshot.png")
126
127
# Capture a specific control
128
button = uiautomation.ButtonControl(Name='Submit')
129
if button.Exists():
130
button.CaptureToImage("button_screenshot.png")
131
```
132
133
### Working with Bitmap Objects
134
135
```python
136
# Capture control to file
137
window = uiautomation.WindowControl(Name='Calculator')
138
if window.Exists():
139
window.CaptureToImage("calculator_window.png")
140
141
# Load bitmap from file
142
loaded_bitmap = uiautomation.Bitmap.FromFile("reference_image.png")
143
```
144
145
### Pixel Color Analysis
146
147
```python
148
# Get pixel color from screen
149
color = uiautomation.GetPixelColor(300, 200)
150
print(f"Pixel color: 0x{color:06X}")
151
152
# Convert to RGB
153
red, green, blue = uiautomation.ColorToRGB(color)
154
print(f"RGB: ({red}, {green}, {blue})")
155
156
# Check if pixel is a specific color
157
if color == uiautomation.Color.RED:
158
print("Pixel is red")
159
160
# Get pixel color from bitmap
161
bitmap = uiautomation.Bitmap.FromFile("image.png")
162
pixel_color = bitmap.GetPixelColor(50, 75)
163
```
164
165
### Visual Verification
166
167
```python
168
def verify_button_color(button_name, expected_color):
169
"""Verify a button has the expected background color."""
170
button = uiautomation.ButtonControl(Name=button_name)
171
if not button.Exists():
172
return False
173
174
# Capture button to temporary file
175
button.CaptureToImage("temp_button.png")
176
bitmap = uiautomation.Bitmap.FromFile("temp_button.png")
177
178
# Check center pixel color (assuming solid color button)
179
center_x = bitmap.Width // 2
180
center_y = bitmap.Height // 2
181
actual_color = bitmap.GetPixelColor(center_x, center_y)
182
183
# Compare colors (with tolerance for slight variations)
184
return color_similar(actual_color, expected_color, tolerance=10)
185
186
def color_similar(color1, color2, tolerance=5):
187
"""Check if two colors are similar within tolerance."""
188
r1, g1, b1 = uiautomation.ColorToRGB(color1)
189
r2, g2, b2 = uiautomation.ColorToRGB(color2)
190
191
return (abs(r1 - r2) <= tolerance and
192
abs(g1 - g2) <= tolerance and
193
abs(b1 - b2) <= tolerance)
194
195
# Use the verification function
196
if verify_button_color('OK', uiautomation.Color.GREEN):
197
print("OK button has correct green color")
198
```
199
200
### Image-Based Control Location
201
202
```python
203
def find_control_by_image(reference_image_path, tolerance=0.95):
204
"""Find control by matching against reference image."""
205
# Note: Screen capture would need to be implemented using Win32API
206
# This is a conceptual example
207
current_screen = uiautomation.Bitmap.FromFile("current_screen.png")
208
reference = uiautomation.Bitmap.FromFile(reference_image_path)
209
210
# Simple template matching (this is a conceptual example)
211
# Real implementation would use image processing algorithms
212
for y in range(current_screen.Height - reference.Height):
213
for x in range(current_screen.Width - reference.Width):
214
if image_matches_at_position(current_screen, reference, x, y, tolerance):
215
return uiautomation.Point(x + reference.Width // 2,
216
y + reference.Height // 2)
217
return None
218
219
def image_matches_at_position(source, template, x, y, tolerance):
220
"""Check if template matches source at given position."""
221
# Simplified matching logic - real implementation would be more sophisticated
222
matching_pixels = 0
223
total_pixels = template.Width * template.Height
224
225
for ty in range(template.Height):
226
for tx in range(template.Width):
227
src_color = source.GetPixelColor(x + tx, y + ty)
228
tpl_color = template.GetPixelColor(tx, ty)
229
if color_similar(src_color, tpl_color, tolerance=10):
230
matching_pixels += 1
231
232
return (matching_pixels / total_pixels) >= tolerance
233
```
234
235
### Screenshot Comparison
236
237
```python
238
def compare_screenshots(image1_path, image2_path, difference_output=None):
239
"""Compare two screenshots and optionally generate difference image."""
240
img1 = uiautomation.Bitmap.FromFile(image1_path)
241
img2 = uiautomation.Bitmap.FromFile(image2_path)
242
243
if img1.Width != img2.Width or img1.Height != img2.Height:
244
print("Images have different dimensions")
245
return False
246
247
differences = 0
248
total_pixels = img1.Width * img1.Height
249
250
# Create difference image if requested
251
diff_bitmap = None
252
if difference_output:
253
diff_bitmap = uiautomation.Bitmap.FromFile(image1_path) # Copy of first image
254
255
for y in range(img1.Height):
256
for x in range(img1.Width):
257
color1 = img1.GetPixelColor(x, y)
258
color2 = img2.GetPixelColor(x, y)
259
260
if color1 != color2:
261
differences += 1
262
if diff_bitmap:
263
# Highlight differences in red
264
diff_bitmap.SetPixelColor(x, y, uiautomation.Color.RED)
265
266
if diff_bitmap and difference_output:
267
diff_bitmap.ToFile(difference_output)
268
269
similarity = 1.0 - (differences / total_pixels)
270
print(f"Images are {similarity * 100:.2f}% similar")
271
return similarity > 0.95 # 95% similarity threshold
272
273
# Compare before and after screenshots
274
compare_screenshots("before.png", "after.png", "differences.png")
275
```
276
277
### Automated Visual Testing
278
279
```python
280
def visual_regression_test(test_name, control_selector):
281
"""Perform visual regression testing on a control."""
282
# Define reference image path
283
reference_path = f"reference_images/{test_name}.png"
284
current_path = f"current_images/{test_name}.png"
285
286
# Find and capture control
287
control = uiautomation.FindControl(**control_selector)
288
if not control.Exists():
289
print(f"Control not found for test: {test_name}")
290
return False
291
292
# Capture current state
293
control.CaptureToImage(current_path)
294
295
# Compare with reference if it exists
296
try:
297
if compare_screenshots(reference_path, current_path):
298
print(f"✓ Visual test passed: {test_name}")
299
return True
300
else:
301
print(f"✗ Visual test failed: {test_name}")
302
return False
303
except FileNotFoundError:
304
print(f"Reference image not found for {test_name}, creating baseline")
305
# Copy current as reference for future tests
306
import shutil
307
shutil.copy(current_path, reference_path)
308
return True
309
310
# Run visual tests
311
visual_regression_test("login_button", {"Name": "Login", "ControlType": uiautomation.ControlType.ButtonControl})
312
visual_regression_test("main_window", {"Name": "Application", "ControlType": uiautomation.ControlType.WindowControl})
313
```