fgreenlee_sfemu commited on
Commit ·
d03ebb8
1
Parent(s): 808a1c6
Add composite_cursor.py and .gitignore
Browse filescomposite_cursor.py provides a utility for placing cursors onto
screenshots with correct hotspot alignment and alpha compositing.
Made-with: Cursor
- .gitignore +5 -0
- composite_cursor.py +135 -0
.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cursor_dataset/
|
| 2 |
+
sample_screenshots/
|
| 3 |
+
.cursor/
|
| 4 |
+
__pycache__/
|
| 5 |
+
*.pyc
|
composite_cursor.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# /// script
|
| 3 |
+
# requires-python = ">=3.10"
|
| 4 |
+
# dependencies = [
|
| 5 |
+
# "Pillow>=10.0",
|
| 6 |
+
# "datasets>=2.14",
|
| 7 |
+
# ]
|
| 8 |
+
# ///
|
| 9 |
+
"""Composite a cursor from the Fraser/cursors dataset onto a screenshot.
|
| 10 |
+
|
| 11 |
+
Usage:
|
| 12 |
+
uv run composite_cursor.py sample_screenshots/some_image.png
|
| 13 |
+
|
| 14 |
+
Produces: sample_screenshots/some_image_cursored.png
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import random
|
| 18 |
+
import sys
|
| 19 |
+
from pathlib import Path
|
| 20 |
+
|
| 21 |
+
from datasets import load_dataset
|
| 22 |
+
from PIL import Image
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def load_cursors(os_filter=None):
|
| 26 |
+
"""Load the cursor dataset, optionally filtering by OS."""
|
| 27 |
+
ds = load_dataset("Fraser/cursors", split="train")
|
| 28 |
+
if os_filter:
|
| 29 |
+
ds = ds.filter(lambda r: r["os"] == os_filter)
|
| 30 |
+
return ds
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def composite_cursor(
|
| 34 |
+
screenshot: Image.Image,
|
| 35 |
+
cursor_frame: Image.Image,
|
| 36 |
+
hotspot_x: float,
|
| 37 |
+
hotspot_y: float,
|
| 38 |
+
target_x: int,
|
| 39 |
+
target_y: int,
|
| 40 |
+
cursor_size: int = 32,
|
| 41 |
+
) -> Image.Image:
|
| 42 |
+
"""Place a cursor onto a screenshot so the hotspot lands at (target_x, target_y).
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
screenshot: The background image (any mode, will be converted to RGBA).
|
| 46 |
+
cursor_frame: A single RGBA cursor frame.
|
| 47 |
+
hotspot_x: Normalised hotspot x (0-1 within cursor image).
|
| 48 |
+
hotspot_y: Normalised hotspot y (0-1 within cursor image).
|
| 49 |
+
target_x: Pixel x on the screenshot where the hotspot should land.
|
| 50 |
+
target_y: Pixel y on the screenshot where the hotspot should land.
|
| 51 |
+
cursor_size: Desired cursor height in pixels (width scales proportionally).
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
Composited image in the same mode as the input screenshot.
|
| 55 |
+
"""
|
| 56 |
+
orig_mode = screenshot.mode
|
| 57 |
+
canvas = screenshot.convert("RGBA")
|
| 58 |
+
|
| 59 |
+
cw, ch = cursor_frame.size
|
| 60 |
+
scale = cursor_size / max(cw, ch)
|
| 61 |
+
new_w, new_h = max(1, round(cw * scale)), max(1, round(ch * scale))
|
| 62 |
+
cursor_scaled = cursor_frame.convert("RGBA").resize(
|
| 63 |
+
(new_w, new_h), Image.LANCZOS
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
paste_x = target_x - round(hotspot_x * new_w)
|
| 67 |
+
paste_y = target_y - round(hotspot_y * new_h)
|
| 68 |
+
|
| 69 |
+
# Crop cursor if it extends beyond the screenshot edges
|
| 70 |
+
sw, sh = canvas.size
|
| 71 |
+
src_left = max(0, -paste_x)
|
| 72 |
+
src_top = max(0, -paste_y)
|
| 73 |
+
src_right = min(new_w, sw - paste_x)
|
| 74 |
+
src_bottom = min(new_h, sh - paste_y)
|
| 75 |
+
|
| 76 |
+
if src_left >= src_right or src_top >= src_bottom:
|
| 77 |
+
return screenshot
|
| 78 |
+
|
| 79 |
+
cropped = cursor_scaled.crop((src_left, src_top, src_right, src_bottom))
|
| 80 |
+
dst_x = paste_x + src_left
|
| 81 |
+
dst_y = paste_y + src_top
|
| 82 |
+
|
| 83 |
+
canvas.alpha_composite(cropped, (dst_x, dst_y))
|
| 84 |
+
|
| 85 |
+
if orig_mode != "RGBA":
|
| 86 |
+
canvas = canvas.convert(orig_mode)
|
| 87 |
+
return canvas
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def main():
|
| 91 |
+
if len(sys.argv) < 2:
|
| 92 |
+
print("Usage: uv run composite_cursor.py <screenshot.png> [x y] [cursor_size]")
|
| 93 |
+
print(" x, y: click-point position (default: random)")
|
| 94 |
+
print(" cursor_size: cursor height in pixels (default: 32)")
|
| 95 |
+
sys.exit(1)
|
| 96 |
+
|
| 97 |
+
screenshot_path = Path(sys.argv[1])
|
| 98 |
+
screenshot = Image.open(screenshot_path)
|
| 99 |
+
sw, sh = screenshot.size
|
| 100 |
+
|
| 101 |
+
if len(sys.argv) >= 4:
|
| 102 |
+
tx, ty = int(sys.argv[2]), int(sys.argv[3])
|
| 103 |
+
else:
|
| 104 |
+
tx = random.randint(int(sw * 0.05), int(sw * 0.95))
|
| 105 |
+
ty = random.randint(int(sh * 0.05), int(sh * 0.95))
|
| 106 |
+
|
| 107 |
+
cursor_size = int(sys.argv[4]) if len(sys.argv) >= 5 else 32
|
| 108 |
+
|
| 109 |
+
print(f"Screenshot: {screenshot_path} ({sw}x{sh})")
|
| 110 |
+
print(f"Loading cursors...")
|
| 111 |
+
ds = load_cursors()
|
| 112 |
+
|
| 113 |
+
# Pick a random static cursor
|
| 114 |
+
static = [i for i in range(len(ds)) if len(ds[i]["frames"]) == 1]
|
| 115 |
+
idx = random.choice(static)
|
| 116 |
+
row = ds[idx]
|
| 117 |
+
|
| 118 |
+
cursor_frame = row["frames"][0]
|
| 119 |
+
print(f"Cursor: {row['cursor_type']} / {row['os']} / {row['variant']}")
|
| 120 |
+
print(f"Hotspot: ({row['hotspot_x']:.2f}, {row['hotspot_y']:.2f})")
|
| 121 |
+
print(f"Placing at: ({tx}, {ty}), size={cursor_size}px")
|
| 122 |
+
|
| 123 |
+
result = composite_cursor(
|
| 124 |
+
screenshot, cursor_frame,
|
| 125 |
+
row["hotspot_x"], row["hotspot_y"],
|
| 126 |
+
tx, ty, cursor_size,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
out_path = screenshot_path.with_stem(screenshot_path.stem + "_cursored")
|
| 130 |
+
result.save(out_path)
|
| 131 |
+
print(f"Saved: {out_path}")
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
main()
|