from PIL import Image

full = Image.open("captures/20260913_102100_639_AFTER_CLICK_ATLAS_NODE.png")
btn = Image.open("captures/traverse_button_crop.png")

bw, bh = btn.size
fw, fh = full.size

# Search for the top-left of btn in full
# Grab a 10x10 patch from center of btn
btn_patch = [btn.getpixel((x, y))[:3] for x in range(100, 110) for y in range(40, 50)]

best_diff = float("inf")
best_loc = (0, 0)

# We know it's roughly x in 800..1300, y in 400..800
for y in range(400, 800):
    for x in range(800, 1300):
        # compare patch
        diff = 0
        match = True
        idx = 0
        for px in range(10):
            for py in range(10):
                r, g, b = full.getpixel((x + 100 + px, y + 40 + py))[:3]
                br, bg, bb = btn_patch[idx]
                idx += 1
                if abs(r - br) > 10 or abs(g - bg) > 10 or abs(b - bb) > 10:
                    match = False
                    break
            if not match:
                break
        if match:
            best_loc = (x, y)
            print(f"Exact match found at top-left: ({x}, {y})")
            center = (x + bw // 2, y + bh // 2)
            print(f"Button center: {center}")
            break
    if match:
        break
