Add cdcrop_resize.py: anti-aliased downscale + optional unsharp mask
Automates the resize step of the Photoshop workflow. Always box-reduces before the Lanczos resample (reducing_gap=3.0) instead of Pillow's default single-pass filter, which is the source of moire on large downscales - replacing the manual "blur if I see moire" judgment call.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
cdcrop_resize.py - resize cropped cover art to a web-sized PNG, the way you'd
|
||||
do it by hand in Photoshop: resize down, optional unsharp mask.
|
||||
|
||||
Downscaling a halftone-printed scan by ~8-9x (a typical cover goes from a few
|
||||
thousand px to an 800 px long edge) is exactly the regime where a plain
|
||||
Lanczos resize aliases against the print's halftone pattern and shows up as
|
||||
moire - which is why you've been pre-blurring by hand when you spot it.
|
||||
Pillow's resize() only avoids that if you ask for it (reducing_gap >= 3.0
|
||||
makes it box-reduce in whole-pixel steps before the final filtered resample,
|
||||
instead of one unfiltered pass); this script always asks for it, so the
|
||||
moire shouldn't show up in the first place. --pre-blur is kept as a manual
|
||||
fallback for any image where that still isn't enough.
|
||||
|
||||
Usage:
|
||||
python cdcrop_resize.py IN.png [IN2.png ...] -o OUTDIR
|
||||
python cdcrop_resize.py INPUT_FOLDER -o OUTDIR
|
||||
Options:
|
||||
--long-edge PX target size of the longest edge (default 800)
|
||||
--pre-blur SIGMA extra gaussian blur before resizing (default 0, off;
|
||||
manual moire fallback if a cover still needs it)
|
||||
--sharpen apply an unsharp mask after resizing
|
||||
--sharpen-radius PX unsharp mask radius (default 1.0)
|
||||
--sharpen-percent PCT unsharp mask strength (default 60)
|
||||
--sharpen-threshold N unsharp mask threshold (default 2)
|
||||
No DPI tag is written - the scan's DPI doesn't describe a 800 px derivative.
|
||||
Requires: pip install pillow
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
|
||||
from PIL import Image, ImageFilter
|
||||
|
||||
RESIZE_GAP = 3.0 # >=3.0: Pillow's box-reduce pre-pass is indistinguishable from a fair resample
|
||||
|
||||
|
||||
def resize_one(path, outdir, long_edge, pre_blur, sharpen, sharpen_radius, sharpen_percent, sharpen_threshold):
|
||||
im = Image.open(path)
|
||||
mode = im.mode
|
||||
w, h = im.size
|
||||
scale = long_edge / max(w, h)
|
||||
if scale >= 1.0:
|
||||
print(f" {os.path.basename(path)} {w}x{h} already <= {long_edge}px, copied as-is")
|
||||
new_w, new_h = w, h
|
||||
else:
|
||||
new_w, new_h = round(w * scale), round(h * scale)
|
||||
if pre_blur > 0:
|
||||
im = im.filter(ImageFilter.GaussianBlur(pre_blur))
|
||||
im = im.resize((new_w, new_h), resample=Image.LANCZOS, reducing_gap=RESIZE_GAP)
|
||||
print(f" {os.path.basename(path)} {w}x{h} -> {new_w}x{new_h}"
|
||||
+ (f" (pre-blur {pre_blur:g})" if pre_blur > 0 else ""))
|
||||
if sharpen:
|
||||
im = im.filter(ImageFilter.UnsharpMask(radius=sharpen_radius, percent=sharpen_percent,
|
||||
threshold=sharpen_threshold))
|
||||
if im.mode != mode:
|
||||
im = im.convert(mode)
|
||||
out_path = os.path.join(outdir, os.path.basename(path))
|
||||
im.save(out_path, format="PNG")
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument("inputs", nargs="+", help="cropped PNGs, or a folder of them")
|
||||
ap.add_argument("-o", "--outdir", required=True)
|
||||
ap.add_argument("--long-edge", type=int, default=800)
|
||||
ap.add_argument("--pre-blur", type=float, default=0.0)
|
||||
ap.add_argument("--sharpen", action="store_true")
|
||||
ap.add_argument("--sharpen-radius", type=float, default=1.0)
|
||||
ap.add_argument("--sharpen-percent", type=int, default=60)
|
||||
ap.add_argument("--sharpen-threshold", type=int, default=2)
|
||||
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:
|
||||
resize_one(f, a.outdir, a.long_edge, a.pre_blur, a.sharpen,
|
||||
a.sharpen_radius, a.sharpen_percent, a.sharpen_threshold)
|
||||
print(f"\n{len(files)} cover(s) resized to {a.outdir}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user