from PIL import Image
import os

audit_dir = r'C:\allgifted\mathapi11v2\_work_barmodels\audit_r6'
fpath = os.path.join(audit_dir, 'imp1pg035q6.png')

img = Image.open(fpath)
print(f"Image: {img.size}, mode={img.mode}")

if img.mode == 'RGBA':
    # Get non-transparent pixels
    pixels = list(img.getdata())
    opaque = [(i % img.width, i // img.width, r, g, b) for i, (r, g, b, a) in enumerate(pixels) if a > 128]
else:
    pixels = list(img.getdata())
    opaque = [(i % img.width, i // img.width, r, g, b) for i, (r, g, b) in enumerate(pixels) if (r < 240 or g < 240 or b < 240)]

if opaque:
    xs = [p[0] for p in opaque]
    ys = [p[1] for p in opaque]
    min_x, max_x = min(xs), max(xs)
    min_y, max_y = min(ys), max(ys)
    w = max_x - min_x
    h = max_y - min_y
    aspect = w / h if h > 0 else 0
    center_x = (min_x + max_x) / 2
    center_y = (min_y + max_y) / 2

    print(f"Non-white bounding box: ({min_x},{min_y}) -> ({max_x},{max_y}), {w}x{h}")
    print(f"Aspect ratio: {aspect:.3f}")

    # Check pixel distribution in quadrants to guess shape
    # For a diamond, the shape is rotated 45 degrees
    # For a square, aspect ~1.0
    # For a circle, aspect ~1.0 but corners are empty

    # Count pixels in each quadrant relative to center
    q1 = q2 = q3 = q4 = 0
    for x, y, r, g, b in opaque:
        if x >= center_x and y >= center_y:
            q1 += 1
        elif x < center_x and y >= center_y:
            q2 += 1
        elif x < center_x and y < center_y:
            q3 += 1
        elif x >= center_x and y < center_y:
            q4 += 1

    total = len(opaque)
    print(f"Quadrant distribution: Q1={q1/total*100:.1f}% Q2={q2/total*100:.1f}% Q3={q3/total*100:.1f}% Q4={q4/total*100:.1f}%")

    # Check corners - in a circle, corners of bounding box should be empty
    corners_empty = 0
    corner_size = max(5, min(w, h) // 10)
    corners = [
        (range(min_x, min_x + corner_size), range(min_y, min_y + corner_size), "top-left"),
        (range(max_x - corner_size, max_x), range(min_y, min_y + corner_size), "top-right"),
        (range(min_x, min_x + corner_size), range(max_y - corner_size, max_y), "bottom-left"),
        (range(max_x - corner_size, max_x), range(max_y - corner_size, max_y), "bottom-right"),
    ]
    for x_range, y_range, name in corners:
        corner_pixels = sum(1 for (x, y, _, _, _) in opaque if x in x_range and y in y_range)
        total_corner_area = len(x_range) * len(y_range)
        empty_ratio = 1 - (corner_pixels / total_corner_area if total_corner_area > 0 else 1)
        print(f"  {name} corner: {corner_pixels} filled / {total_corner_area} area = {empty_ratio*100:.0f}% empty")
        if empty_ratio > 0.5:
            corners_empty += 1

    # Interpretation
    print()
    if corners_empty >= 3 and abs(aspect - 1.0) < 0.2:
        print("Likely CIRCLE (aspect ~1, corners empty)")
    elif corners_empty <= 1 and abs(aspect - 1.0) < 0.15:
        print("Likely SQUARE (aspect ~1, corners filled)")
    elif corners_empty <= 2 and aspect > 1.2:
        print("Likely RECTANGLE (wide aspect, corners filled)")
    elif corners_empty <= 2 and aspect < 0.8:
        print("Likely RECTANGLE (tall aspect, corners filled)")
    elif abs(aspect - 1.0) < 0.15 and corners_empty >= 1:
        print("Likely TRIANGLE or DIAMOND")
        # Diamond has more pixels in center, triangle has asymmetric distribution
        if abs(q1 - q2) < total * 0.1 and abs(q3 - q4) < total * 0.1:
            print("  Symmetric distribution -> Likely DIAMOND")
        else:
            print("  Asymmetric distribution -> Likely TRIANGLE")
    print(f"DB says: square (ci=1), Chunk says: diamond (ci=3)")
else:
    print("No non-white pixels found")
