"""Inspect saved tool outputs independently. Requires paired downloads + Pillow."""
from pathlib import Path
from PIL import Image
import hashlib,json,datetime
HERE=Path(__file__).resolve().parent
sha=lambda b:hashlib.sha256(b).hexdigest()
outputs=[]
for p in sorted(HERE.glob('*-result.png')):
    im=Image.open(p);rgba=im.convert('RGBA');data=rgba.tobytes()
    probes=[(im.width//4,im.height//4),(im.width*3//4,im.height//4),(im.width//4,im.height*3//4),(im.width*3//4,im.height*3//4)]
    outputs.append({'file':p.name,'width':im.width,'height':im.height,'bytes':p.stat().st_size,'sha256':sha(p.read_bytes()),'rgba_sha256':sha(data),'non_opaque_pixels':sum(data[i]!=255 for i in range(3,len(data),4)),'mode':im.mode,'exif_orientation':im.getexif().get(274),'probes':[{'xy':list(pt),'rgba':list(rgba.getpixel(pt))}for pt in probes]})
ref=Image.open(HERE/'detail-reference.png').convert('RGB');out=Image.open(HERE/'low-quality-result.png').convert('RGB')
changed=sum(a!=b for a,b in zip(ref.getdata(),out.getdata()));total=ref.width*ref.height
report={'checked_at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'method':'Pillow independently decodes the actual PNG bytes offered by the browser tool. RGBA SHA compared with browser pixel report. Browser versions not recorded.','outputs':outputs,'pre_jpeg_reference_comparison':{'reference_file':'detail-reference.png','output_file':'low-quality-result.png','changed_pixels':changed,'total_pixels':total,'ratio':changed/total,'formula':'pixels with any RGB channel different / (width * height); both inputs are paired downloadable images, inspected with Pillow. Different from browser JPEG to PNG identity check.'}}
(HERE/'output-verification.json').write_text(json.dumps(report,indent=2)+'\n',encoding='utf-8')
print(json.dumps(report))
