|
| 1 | +from PIL import Image, ImageDraw |
| 2 | +from playwright.sync_api import Page |
| 3 | + |
| 4 | + |
| 5 | +def draw_mouse_pointer(image: Image.Image, x: int, y: int) -> Image.Image: |
| 6 | + """ |
| 7 | + Draws a semi-transparent mouse pointer at (x, y) on the image. |
| 8 | + Returns a new image with the pointer drawn. |
| 9 | +
|
| 10 | + Args: |
| 11 | + image: The image to draw the mouse pointer on. |
| 12 | + x: The x coordinate for the mouse pointer. |
| 13 | + y: The y coordinate for the mouse pointer. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + A new image with the mouse pointer drawn. |
| 17 | + """ |
| 18 | + pointer_size = 20 # Length of the pointer |
| 19 | + overlay = image.convert("RGBA").copy() |
| 20 | + draw = ImageDraw.Draw(overlay) |
| 21 | + |
| 22 | + # Define pointer shape (a simple arrow) |
| 23 | + pointer_shape = [ |
| 24 | + (x, y), |
| 25 | + (x + pointer_size, y + pointer_size // 2), |
| 26 | + (x + pointer_size // 2, y + pointer_size // 2), |
| 27 | + (x + pointer_size // 2, y + pointer_size), |
| 28 | + ] |
| 29 | + |
| 30 | + draw.polygon(pointer_shape, fill=(0, 0, 0, 128)) # 50% transparent black |
| 31 | + |
| 32 | + return Image.alpha_composite(image.convert("RGBA"), overlay) |
| 33 | + |
| 34 | + |
| 35 | +def draw_arrowhead(draw, start, end, arrow_length=15, arrow_angle=30): |
| 36 | + from math import atan2, cos, radians, sin |
| 37 | + |
| 38 | + angle = atan2(end[1] - start[1], end[0] - start[0]) |
| 39 | + left = ( |
| 40 | + end[0] - arrow_length * cos(angle - radians(arrow_angle)), |
| 41 | + end[1] - arrow_length * sin(angle - radians(arrow_angle)), |
| 42 | + ) |
| 43 | + right = ( |
| 44 | + end[0] - arrow_length * cos(angle + radians(arrow_angle)), |
| 45 | + end[1] - arrow_length * sin(angle + radians(arrow_angle)), |
| 46 | + ) |
| 47 | + draw.line([end, left], fill="red", width=4) |
| 48 | + draw.line([end, right], fill="red", width=4) |
| 49 | + |
| 50 | + |
| 51 | +def draw_click_indicator(image: Image.Image, x: int, y: int) -> Image.Image: |
| 52 | + """ |
| 53 | + Draws a click indicator (+ shape with disconnected lines) at (x, y) on the image. |
| 54 | + Returns a new image with the click indicator drawn. |
| 55 | +
|
| 56 | + Args: |
| 57 | + image: The image to draw the click indicator on. |
| 58 | + x: The x coordinate for the click indicator. |
| 59 | + y: The y coordinate for the click indicator. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + A new image with the click indicator drawn. |
| 63 | + """ |
| 64 | + line_length = 10 # Length of each line segment |
| 65 | + gap = 4 # Gap from center point |
| 66 | + line_width = 2 # Thickness of lines |
| 67 | + |
| 68 | + overlay = image.convert("RGBA").copy() |
| 69 | + draw = ImageDraw.Draw(overlay) |
| 70 | + |
| 71 | + # Draw 4 lines forming a + shape with gaps in the center |
| 72 | + # Each line has a white outline and black center for visibility on any background |
| 73 | + |
| 74 | + # Top line |
| 75 | + draw.line( |
| 76 | + [(x, y - gap - line_length), (x, y - gap)], fill=(255, 255, 255, 200), width=line_width + 2 |
| 77 | + ) # White outline |
| 78 | + draw.line( |
| 79 | + [(x, y - gap - line_length), (x, y - gap)], fill=(0, 0, 0, 255), width=line_width |
| 80 | + ) # Black center |
| 81 | + |
| 82 | + # Bottom line |
| 83 | + draw.line( |
| 84 | + [(x, y + gap), (x, y + gap + line_length)], fill=(255, 255, 255, 200), width=line_width + 2 |
| 85 | + ) # White outline |
| 86 | + draw.line( |
| 87 | + [(x, y + gap), (x, y + gap + line_length)], fill=(0, 0, 0, 255), width=line_width |
| 88 | + ) # Black center |
| 89 | + |
| 90 | + # Left line |
| 91 | + draw.line( |
| 92 | + [(x - gap - line_length, y), (x - gap, y)], fill=(255, 255, 255, 200), width=line_width + 2 |
| 93 | + ) # White outline |
| 94 | + draw.line( |
| 95 | + [(x - gap - line_length, y), (x - gap, y)], fill=(0, 0, 0, 255), width=line_width |
| 96 | + ) # Black center |
| 97 | + |
| 98 | + # Right line |
| 99 | + draw.line( |
| 100 | + [(x + gap, y), (x + gap + line_length, y)], fill=(255, 255, 255, 200), width=line_width + 2 |
| 101 | + ) # White outline |
| 102 | + draw.line( |
| 103 | + [(x + gap, y), (x + gap + line_length, y)], fill=(0, 0, 0, 255), width=line_width |
| 104 | + ) # Black center |
| 105 | + |
| 106 | + return Image.alpha_composite(image.convert("RGBA"), overlay) |
| 107 | + |
| 108 | + |
| 109 | +def zoom_webpage(page: Page, zoom_factor: float = 1.5): |
| 110 | + """ |
| 111 | + Zooms the webpage to the specified zoom factor. |
| 112 | +
|
| 113 | + NOTE: Click actions with bid doesn't work properly when zoomed in. |
| 114 | +
|
| 115 | + Args: |
| 116 | + page: The Playwright Page object. |
| 117 | + zoom_factor: The zoom factor to apply (default is 1.5). |
| 118 | +
|
| 119 | + Returns: |
| 120 | + Page: The modified Playwright Page object. |
| 121 | +
|
| 122 | + Raises: |
| 123 | + ValueError: If zoom_factor is less than or equal to 0. |
| 124 | + """ |
| 125 | + |
| 126 | + if zoom_factor <= 0: |
| 127 | + raise ValueError("Zoom factor must be greater than 0.") |
| 128 | + |
| 129 | + page.evaluate(f"document.documentElement.style.zoom='{zoom_factor*100}%'") |
| 130 | + return page |
0 commit comments