from PIL import Image

def find_labels(image_path):
    img = Image.open(image_path)
    w, h = img.size
    labels = []
    for y in range(100, 1000, 5):
        for x in range(100, 2000, 5):
            p = img.getpixel((x, y))
            if p[0] > 180 and p[1] > 180 and p[2] > 180:
                p_bg = img.getpixel((max(0, x - 25), y))
                if p_bg[0] < 30 and p_bg[1] < 30 and p_bg[2] < 30:
                    labels.append((x, y))

    clusters = []
    for pt in labels:
        merged = False
        for c in clusters:
            if abs(pt[0] - c['x']) < 100 and abs(pt[1] - c['y']) < 30:
                c['pts'].append(pt)
                c['x'] = sum(p[0] for p in c['pts']) // len(c['pts'])
                c['y'] = sum(p[1] for p in c['pts']) // len(c['pts'])
                merged = True
                break
        if not merged:
            clusters.append({'x': pt[0], 'y': pt[1], 'pts': [pt]})

    print(f"Found {len(clusters)} text clusters in {image_path}:")
    for i, c in enumerate(clusters):
        if len(c['pts']) >= 5:
            print(f"  Cluster {i}: ({c['x']}, {c['y']}), count={len(c['pts'])}")

if __name__ == "__main__":
    find_labels("captures/after_move_a.png")
