From ab9aa05ac24cdbe237b296dfab357cd4950620dc Mon Sep 17 00:00:00 2001 From: aarbit Date: Tue, 10 Nov 2026 23:41:52 -0600 Subject: [PATCH] Add --black-point to cdcrop_levels.py A per-cover escape hatch, same pattern as --pre-blur in cdcrop_resize.py: off by default since the 12-cover fit only supports the flat gamma as a general default, not a black-point push - turn it on per cover as needed. --- cdcrop_levels.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/cdcrop_levels.py b/cdcrop_levels.py index 68731fe..1099b1b 100644 --- a/cdcrop_levels.py +++ b/cdcrop_levels.py @@ -13,11 +13,18 @@ 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. +Some covers want their black point pushed deeper too (a separate move from +gamma - the Levels black slider, not the midtone one); that's left off by +default since it's a per-cover call, not something the 12-cover fit supports +as a general default. Turn it on with --black-point when a cover needs it. + 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) + --gamma G midtone darkening, out = 255*(in/255)**G (default 1.2; 1.0 = no change) + --black-point N clip N and below to black, stretch the rest, before gamma + (default 0, off; 0-254) Requires: pip install pillow numpy """ import argparse @@ -30,11 +37,13 @@ from PIL import Image DEFAULT_GAMMA = 1.2 -def apply_gamma(im, gamma): +def apply_levels(im, gamma, black_point): 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 + arr = np.asarray(rgb, dtype=np.float32) + if black_point > 0: + arr = np.clip(arr - black_point, 0, None) * (255.0 / (255 - black_point)) + arr = 255.0 * np.clip(arr / 255.0, 0, 1) ** gamma out = Image.fromarray(np.clip(arr, 0, 255).astype(np.uint8), mode="RGB") if has_alpha: out = out.convert("RGBA") @@ -44,11 +53,12 @@ def apply_gamma(im, gamma): return out -def levels_one(path, outdir, gamma): +def levels_one(path, outdir, gamma, black_point): im = Image.open(path) dpi = im.info.get("dpi") - out = apply_gamma(im, gamma) - print(f" {os.path.basename(path)} gamma {gamma:g}") + out = apply_levels(im, gamma, black_point) + bp_note = f", black point {black_point}" if black_point > 0 else "" + print(f" {os.path.basename(path)} gamma {gamma:g}{bp_note}") 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) @@ -58,7 +68,10 @@ def main(): 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) + ap.add_argument("--black-point", type=int, default=0) a = ap.parse_args() + if not 0 <= a.black_point < 255: + ap.error("--black-point must be 0-254") files = [] for p in a.inputs: @@ -71,7 +84,7 @@ def main(): os.makedirs(a.outdir, exist_ok=True) for f in files: - levels_one(f, a.outdir, a.gamma) + levels_one(f, a.outdir, a.gamma, a.black_point) print(f"\n{len(files)} cover(s) leveled to {a.outdir}")