"""Controlled, non-generative image experiment. No network or user files.
Run: python3 -m pip install Pillow==12.3.0
Then: python3 reproducir.py (writes only the named reference/blur/sharpen files).
NASA source and attribution: https://svs.gsfc.nasa.gov/30613
"""
from pathlib import Path
import hashlib,json
from PIL import Image,ImageFilter,ImageChops,__version__

def mse(a,b):
 h=ImageChops.difference(a,b).histogram()
 return sum((i%256)**2*n for i,n in enumerate(h))/(a.width*a.height*3)

def run(folder):
 folder=Path(folder)
 origin=folder/'nasa-origen.jpg'
 reference=Image.open(origin).convert('RGB').resize((512,512),Image.Resampling.LANCZOS)
 blurred=reference.filter(ImageFilter.GaussianBlur(radius=1.5))
 sharpened=blurred.filter(ImageFilter.UnsharpMask(radius=2,percent=150,threshold=3))
 files={'referencia.png':reference,'desenfoque.png':blurred,'nitidez.png':sharpened}
 for name,im in files.items():im.save(folder/name)
 data={'source':'https://svs.gsfc.nasa.gov/30613','sourceFile':'https://svs.gsfc.nasa.gov/vis/a030000/a030600/a030613/blue_marble_apollo_17_19721207_print.jpg','credit':'Earth Science and Remote Sensing Unit, NASA Johnson Space Center','usage':'https://www.nasa.gov/nasa-brand-center/images-and-media/','editorialUse':'Educational comparison; no NASA endorsement. Original source preserved.','software':'Pillow '+__version__,'parameters':{'reference':'RGB, 512x512, LANCZOS','degradation':'GaussianBlur radius=1.5','improvement':'UnsharpMask radius=2, percent=150, threshold=3'},'mseRGB8bit':{'blurredVsReference':round(mse(reference,blurred),6),'sharpenedVsReference':round(mse(reference,sharpened),6)},'limitations':'A numerical reference-error comparison, not a perceptual quality score, AI benchmark or recovery of authentic missing detail.','files':{name:{'sha256':hashlib.sha256((folder/name).read_bytes()).hexdigest(),'bytes':(folder/name).stat().st_size} for name in ['nasa-origen.jpg',*files]}}
 (folder/'registro.json').write_text(json.dumps(data,ensure_ascii=False,indent=2)+'\n')
 return data
if __name__=='__main__':print(json.dumps(run(Path(__file__).resolve().parent),indent=2))
