bamabound-guides / sharpness_grid.py
conan123's picture
Card v8: sharpness_vs_exposure config + script
8efc056 verified
Raw History Blame Contribute Delete
2.04 kB
#!/usr/bin/env python3
"""Sharpness score vs exposure and blur on synthetic photos.
Shows that the raw variance-of-Laplacian score used by many "is my photo
sharp enough?" checks falls with exposure even when nothing is blurred,
and that dividing by the image's own intensity variance removes most of
that. Writes one JSON line per (exposure, blur) cell.
python3 sharpness_grid.py > sharpness-vs-exposure-2026-11-11.jsonl
"""
import json
import numpy as np
from PIL import Image, ImageFilter
def scene():
# sky, field, a red banner block, a checker patch: no people
a = np.zeros((1600, 1200, 3), np.uint8)
a[:640] = (120, 180, 230); a[640:] = (60, 140, 60)
yy, xx = np.mgrid[0:1600, 0:1200]
a[((xx // 40 + yy // 40) % 2 == 0) & (yy > 900)] = (200, 200, 200)
a[500:760, 300:900] = (200, 16, 46)
return Image.fromarray(a)
def lap_var(g):
g = g.astype(np.float32)
lap = -4 * g[1:-1, 1:-1] + g[:-2, 1:-1] + g[2:, 1:-1] + g[1:-1, :-2] + g[1:-1, 2:]
return float(lap.var())
def main():
base = scene()
for blur in (0, 1, 2, 4, 9):
img = base.filter(ImageFilter.GaussianBlur(blur)) if blur else base
arr = np.asarray(img).astype(np.float32)
for exposure in (1.0, 0.6, 0.35, 0.15):
g = np.asarray(Image.fromarray((arr * exposure).clip(0, 255).astype(np.uint8))
.convert("L").resize((768, 1024)))
raw = lap_var(g)
contrast = float(g.astype(np.float32).var())
print(json.dumps({
"scene": "synthetic_sky_field_banner_checker",
"blur_sigma_px_at_1200w": blur,
"exposure_scale": exposure,
"mean_luma": round(float(g.mean()), 1),
"laplacian_var": round(raw, 2),
"intensity_var": round(contrast, 2),
"laplacian_var_normalized": round(raw / contrast, 5) if contrast else None,
"analysis_size": "768x1024 grayscale",
}))
if __name__ == "__main__":
main()