(image_path, window_element, image_drawer=None, img=None, scale_factor=None)
| 200 | |
| 201 | |
| 202 | def segment_image(image_path, window_element, image_drawer=None, img=None, scale_factor=None): |
| 203 | if image_path is None: |
| 204 | return |
| 205 | |
| 206 | should_save = False |
| 207 | if image_drawer is None: |
| 208 | should_save = True |
| 209 | img = Image.open(image_path) |
| 210 | image_drawer = ImageDraw.Draw(img) |
| 211 | |
| 212 | # Determine scale factor only once at the top level call |
| 213 | if scale_factor is None: |
| 214 | target_width = 0 |
| 215 | if hasattr(window_element, 'window_screen_rect'): |
| 216 | bx, by, bx2, by2 = window_element.window_screen_rect |
| 217 | target_width = bx2 - bx |
| 218 | scale_factor = get_image_scale_factor(img.width, target_width) |
| 219 | |
| 220 | use_scale_factor = scale_factor if scale_factor is not None else _screen_scaling_factor |
| 221 | |
| 222 | # iterate over all children |
| 223 | for child in getattr(window_element, "children", []): |
| 224 | if getattr(child, "children", None): |
| 225 | segment_image(image_path, child, image_drawer=image_drawer, img=img, scale_factor=use_scale_factor) |
| 226 | |
| 227 | if not child.visible: |
| 228 | continue |
| 229 | |
| 230 | bbox = child.visible_bbox |
| 231 | if not bbox: |
| 232 | continue |
| 233 | |
| 234 | size = child.size |
| 235 | if size is None or size.width == 0 or size.height == 0: |
| 236 | continue |
| 237 | |
| 238 | height_offset = 0 if size.height < 2 else 2 |
| 239 | |
| 240 | # convert to device pixels |
| 241 | x1, y1, x2, y2 = bbox |
| 242 | rx1 = int(x1 * use_scale_factor) |
| 243 | ry1 = int(y1 * use_scale_factor) |
| 244 | rx2 = int(x2 * use_scale_factor) - 1 |
| 245 | ry2 = int(y2 * use_scale_factor) - height_offset + 1 |
| 246 | |
| 247 | if rx2 < rx1: |
| 248 | rx2 = rx1 |
| 249 | if ry2 < ry1: |
| 250 | ry2 = ry1 |
| 251 | |
| 252 | color = color_for_role(getattr(child, "role", "")) |
| 253 | |
| 254 | try: |
| 255 | image_drawer.rectangle([(rx1, ry1), (rx2, ry2)], outline=color, width=2) |
| 256 | except Exception as e: |
| 257 | print(f"Error drawing rectangle: {e}") |
| 258 | |
| 259 | if should_save: |
no test coverage detected