#!/usr/bin/env python3
"""Read one user-selected image locally; report alpha, not visual cutout quality.
Requires Pillow. Does not install packages, upload images or change the source.
"""
from pathlib import Path
import argparse,json,warnings

def inspect(path: Path) -> dict:
    from PIL import Image
    if path.stat().st_size>100*1024*1024:raise ValueError('File exceeds 100 MiB')
    with warnings.catch_warnings():
        warnings.simplefilter('error',Image.DecompressionBombWarning)
        with Image.open(path) as im:
            w,h=im.size
            if w*h>32_000_000:raise ValueError('Image exceeds 32 million pixels')
            original_mode=im.mode
            has_band='A' in im.getbands()
            has_transparency='transparency' in im.info
            frames=getattr(im,'n_frames',1)
            out={'fileName':path.name,'width':w,'height':h,'originalMode':original_mode,'alphaBandPresent':has_band,
                 'transparencyMetadataPresent':has_transparency,'framesDetected':frames,'frameInspected':0,
                 'measurement':'8-bit normalized alpha; first frame only','uploaded':False,'visualEdgeQualityAssessed':False}
            if not has_band and not has_transparency:
                return {**out,'alphaMin':None,'alphaMax':None,'pixelsBelowFullOpacity':None,'status':'no-alpha-or-transparency-metadata'}
            alpha=im.convert('RGBA').getchannel('A');hist=alpha.histogram();lo,hi=alpha.getextrema()
            below=sum(hist[:255]);total=w*h
            return {**out,'alphaMin':lo,'alphaMax':hi,'pixelsBelowFullOpacity':below,'pixelCount':total,
                    'status':'fully-invisible' if hi==0 else 'fully-opaque' if lo==255 else 'some-transparent-pixels'}

def main():
    p=argparse.ArgumentParser(description=__doc__);p.add_argument('image',type=Path);a=p.parse_args()
    try:print(json.dumps(inspect(a.image),ensure_ascii=False,indent=2));return 0
    except Exception as e:print(json.dumps({'error':type(e).__name__,'message':'Could not inspect the selected image safely. Check the file and Pillow installation.'}));return 1
if __name__=='__main__':raise SystemExit(main())
