#!/usr/bin/env python3
import numpy as np
from PIL import Image

im = Image.open('/root/.hermes/image_cache/zrd_cutouts/item_r2c2.png').convert('RGBA')
a = np.array(im)
rgb, al = a[:, :, :3].astype(np.int16), a[:, :, 3]
soft = (al >= 10) & (al <= 250)
print('soft px:', int(soft.sum()))
vals = rgb[soft]
print('soft mean RGB:', vals.mean(axis=0).round(1))
print('soft max RGB:', vals.max(axis=0))
print('soft brightness>120 share:', round((vals.mean(axis=1) > 120).mean(), 3))
# how many soft pixels are bright (would show as halo on dark bg)
# also check fully opaque pixels near edge (within 3px of transparency)
h, w = al.shape
opq = al > 250
tr = al < 10
near = np.zeros((h, w), bool)
from collections import deque
# dilate transparent region by 4 px
d = tr.copy()
for _ in range(4):
    nd = d.copy()
    for dy, dx in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        sy = slice(max(0, dy), h + min(0, dy))
        sx = slice(max(0, dx), w + min(0, dx))
        ty = slice(max(0, -dy), h + min(0, -dy))
        tx = slice(max(0, -dx), w + min(0, -dx))
        nd[ty, tx] |= d[sy, sx]
    d = nd
near = opq & d
print('opaque within 4px of transparent:', int(near.sum()))
nv = rgb[near]
print('  mean RGB:', nv.mean(axis=0).round(1), 'max:', nv.max(axis=0))
print('  bright>120 share:', round((nv.mean(axis=1) > 120).mean(), 3))
