Add cdcrop_levels.py: flat gamma darken fit to Alex's Levels picks
A percentile black/white stretch was tested first and made every sample worse (these scans already use the full 0-255 range, so clipping just blows out endpoints). A flat gamma=1.2 darken, fit against 12 finished covers in singled/manual/, cut the error ~60% with no regressions - used as the default here.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
cdcrop_levels.py - darken midtones with a flat gamma curve, approximating
|
||||
your Photoshop Levels pass.
|
||||
|
||||
A fit against 12 of your finished covers (singled/manual/) showed your levels
|
||||
moves aren't a black/white point stretch - these scans already use nearly the
|
||||
full 0-255 range, so clipping endpoints only blows them out and made every
|
||||
sample worse. What actually matches is a per-channel gamma darken of the
|
||||
midtones; a single flat gamma (same value, all channels, every cover) got
|
||||
close on every sample tested, so that's the starting default here. Covers
|
||||
that need more than the default (darker art, heavier midtone pull) can be
|
||||
re-run with a higher --gamma the same way --pre-blur works in
|
||||
cdcrop_resize.py.
|
||||
|
||||
Usage:
|
||||
python cdcrop_levels.py IN.png [IN2.png ...] -o OUTDIR
|
||||
python cdcrop_levels.py INPUT_FOLDER -o OUTDIR
|
||||
Options:
|
||||
--gamma G midtone darkening, out = 255*(in/255)**G (default 1.2; 1.0 = no change)
|
||||
Requires: pip install pillow numpy
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
DEFAULT_GAMMA = 1.2
|
||||
|
||||
|
||||
def apply_gamma(im, gamma):
|
||||
has_alpha = im.mode in ("RGBA", "LA")
|
||||
rgb = im.convert("RGB") if im.mode != "RGB" else im
|
||||
arr = np.asarray(rgb, dtype=np.float32) / 255.0
|
||||
arr = 255.0 * np.clip(arr, 0, 1) ** gamma
|
||||
out = Image.fromarray(np.clip(arr, 0, 255).astype(np.uint8), mode="RGB")
|
||||
if has_alpha:
|
||||
out = out.convert("RGBA")
|
||||
out.putalpha(im.getchannel("A"))
|
||||
elif im.mode != "RGB":
|
||||
out = out.convert(im.mode)
|
||||
return out
|
||||
|
||||
|
||||
def levels_one(path, outdir, gamma):
|
||||
im = Image.open(path)
|
||||
dpi = im.info.get("dpi")
|
||||
out = apply_gamma(im, gamma)
|
||||
print(f" {os.path.basename(path)} gamma {gamma:g}")
|
||||
kw = {"dpi": tuple(round(float(d)) for d in dpi)} if dpi else {}
|
||||
out.save(os.path.join(outdir, os.path.basename(path)), format="PNG", **kw)
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument("inputs", nargs="+", help="cropped/resized PNGs, or a folder of them")
|
||||
ap.add_argument("-o", "--outdir", required=True)
|
||||
ap.add_argument("--gamma", type=float, default=DEFAULT_GAMMA)
|
||||
a = ap.parse_args()
|
||||
|
||||
files = []
|
||||
for p in a.inputs:
|
||||
if os.path.isdir(p):
|
||||
files += sorted(os.path.join(p, f) for f in os.listdir(p) if f.lower().endswith(".png"))
|
||||
else:
|
||||
files.append(p)
|
||||
if not files:
|
||||
ap.error("no PNG files found")
|
||||
|
||||
os.makedirs(a.outdir, exist_ok=True)
|
||||
for f in files:
|
||||
levels_one(f, a.outdir, a.gamma)
|
||||
print(f"\n{len(files)} cover(s) leveled to {a.outdir}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user