#!/usr/bin/env python3 """ cdcrop.py - split a flatbed scan holding CD covers into individually straightened, tightly cropped PNG files. For each cover it: 1. finds the cover against the scanner background by its outline, 2. measures all four edges to sub-pixel accuracy; edges with no crisp line (folds, lifted or worn paper) are cropped where in-focus art begins, 3. chooses the rotation (auto mode) the way you would by eye: - vertical lines in the art (boxes, text, borders) come out vertical, - horizontal lines running along the top/bottom edge come out level, - the thin wedges any rotation trims cost as little bold art as possible, - otherwise it leans toward the left edge, and flags close calls with an A/B sheet in _review/, 4. crops to the largest rectangle inside all four edges (no background), 5. rotates + crops in ONE Lanczos resample in floating point (no intermediate rounding, no second interpolation pass), 6. writes a lossless PNG in the original colour mode with the original DPI. Nothing else is changed: no colour correction, sharpening, or resizing. Usage: python cdcrop.py SCAN.bmp [SCAN2.bmp ...] -o OUTDIR python cdcrop.py INPUT_FOLDER -o OUTDIR Options: --align auto|left|right|top|bottom default auto --align-override "file.bmp:2=top" per cover: an edge, auto, or an angle like +0.15 --count N covers per scan (default 2) --inset PX minimum px trimmed inside each edge (default 1.0); soft or highlighted edges are trimmed further automatically --no-preview skip preview images Every run appends to OUTDIR/cdcrop_log.csv (angles considered, what was chosen). Requires: pip install opencv-python numpy pillow scipy """ import argparse import json import math import os import sys import cv2 from scipy.ndimage import median_filter import numpy as np from PIL import Image # ----------------------------------------------------------------- loading def load_scan(path): im = Image.open(path) dpi = im.info.get("dpi") mode = im.mode if mode == "P": # palette BMPs: work in RGB, write back as RGB (lossless widening) im = im.convert("RGB") mode = "RGB" if mode not in ("RGB", "L", "RGBA"): raise ValueError(f"{path}: unsupported image mode {mode}") arr = np.asarray(im) return arr, mode, dpi def save_png(arr, mode, dpi, path): im = Image.fromarray(arr, mode=mode) kw = {} if dpi: kw["dpi"] = tuple(round(float(d)) for d in dpi) im.save(path, format="PNG", **kw) # -------------------------------------------------------------- detection # # Covers are found by their outlines, not their colour: a cover's edge is a # crisp transition (paper on glass), while the lid is smooth. This works for # white covers on a white lid and copes with uneven lid shading. def coarse_rects(arr, count, scale=3, grad_thr=2.0, return_shape=False): """Rough cover rectangles: flood-fill the smooth lid from the scan border; whatever the flood can't reach (enclosed by sharp outlines) is a cover. Edges are measured per colour channel (strongest wins), so a cream or tinted cover on a white lid is found by its colour step even when its brightness barely differs.""" chans = [arr[..., c] for c in range(min(3, arr.shape[2]))] if arr.ndim == 3 else [arr] g = None for ch_ in chans: ch_ = ch_.astype(np.float32) h, w = ch_.shape s = cv2.resize(ch_, (w // scale, h // scale), interpolation=cv2.INTER_AREA) s = cv2.GaussianBlur(s, (0, 0), 1.0) gc = np.hypot(cv2.Sobel(s, cv2.CV_32F, 1, 0, ksize=3), cv2.Sobel(s, cv2.CV_32F, 0, 1, ksize=3)) / 8 g = gc if g is None else np.maximum(g, gc) edges = cv2.dilate((g > grad_thr).astype(np.uint8), np.ones((3, 3), np.uint8)) n, lab = cv2.connectedComponents(1 - edges) border = set(np.unique(np.concatenate([lab[0], lab[-1], lab[:, 0], lab[:, -1]]))) - {0} obj = (~np.isin(lab, list(border))).astype(np.uint8) obj = cv2.morphologyEx(obj, cv2.MORPH_OPEN, np.ones((15, 15), np.uint8)) cnts, _ = cv2.findContours(obj, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:count] cnts = [c for c in cnts if cv2.contourArea(c) > 0.02 * obj.size] rects = [cv2.minAreaRect(cv2.convexHull(c).astype(np.float32) * scale + (scale - 1) / 2.0) for c in cnts] if not return_shape: return rects circ = [] for c in cnts: hull = cv2.convexHull(c) A, P = cv2.contourArea(hull), cv2.arcLength(hull, True) circ.append(4 * math.pi * A / (P * P)) # 1.0 circle, 0.785 square return rects, circ def rect_sides(rect): """Return the 4 sides of a cv2 rect as (p0, p1, outward_normal), named left/right/top/bottom in image orientation.""" pts = cv2.boxPoints(rect) c = pts.mean(axis=0) named = {} for i in range(4): p0, p1 = pts[i], pts[(i + 1) % 4] n = (p0 + p1) / 2 - c n /= np.linalg.norm(n) ang = math.degrees(math.atan2(n[1], n[0])) # 0 = right, 90 = down key = ("right" if -45 <= ang < 45 else "bottom" if 45 <= ang < 135 else "top" if -135 <= ang < -45 else "left") named[key] = (p0, p1, n) if len(named) != 4: raise RuntimeError("could not name cover sides") return named def _band(arr, p0, td, n, ts, offs): base = p0[None, :] + ts[:, None] * td[None, :] xs = (base[:, 0:1] + offs[None, :] * n[0]).astype(np.float32) ys = (base[:, 1:2] + offs[None, :] * n[1]).astype(np.float32) b = cv2.remap(arr, xs, ys, cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) return b if b.ndim == 3 else b[..., None] def fit_edge(arr, p0, p1, n, inner=130, outer=60, trim=0.08, max_ang=1.5): """Locate one cover edge near the coarse side p0-p1 (n = outward normal). 1. Sample a band across the side (inner px inside, outer px outside). 2. Search angles/offsets for straight lines along which a sharp transition runs for most of the side (30th-percentile edge strength, so short art features don't count), measured against the local level so busy halftone print doesn't masquerade as an edge. 3. Of the strong candidates near the outline, take the INNERMOST one: the paper's own edge, not its thickness shadow, a cut-edge line, or a page sticking out from underneath. 4. Refine to sub-pixel per sample and fit a robust line. Returns (point, unit direction, stats) or None.""" Ls = float(np.linalg.norm(p1 - p0)); td = (p1 - p0) / Ls offs = np.arange(-inner, outer + 1, 1.0, dtype=np.float32) ts = np.arange(trim * Ls, (1 - trim) * Ls, 4.0) band = _band(arr, p0, td, n, ts, offs) d = (cv2.Sobel(band, cv2.CV_32F, 1, 0, ksize=3) / 4) dabs = np.abs(d).sum(axis=2) if d.ndim == 3 else np.abs(d) tc = ts.mean() best_prof, best_ang = None, None angs = np.arange(-max_ang, max_ang + 1e-9, 0.05) S = np.empty((len(angs), len(offs)), np.float32) for k, ang in enumerate(angs): sh = (ts - tc) * math.tan(math.radians(ang)) M = np.empty_like(dabs) for i in range(len(ts)): M[i] = np.interp(offs + sh[i], offs, dabs[i], left=0, right=0) S[k] = np.percentile(M, 30, axis=0) raw = S.max(axis=0) # subtract the local level: textured art has a high, flat floor of # gradient everywhere; a real edge stands up out of it. prof = raw - median_filter(raw, size=31, mode="nearest") pk = [j for j in range(1, len(offs) - 1) if prof[j] >= prof[j - 1] and prof[j] > prof[j + 1] and prof[j] >= 0.5 * prof.max()] if not pk: return None # Group candidate lines that lie within 30 px of each other. A narrow # group is one edge plus its junk (shadow, cut-edge line, a page peeking # out); a gap wider than that means a printed border or frame INSIDE the # cover. So: take the outermost group with a strong line, and within it # the innermost line - the paper's own edge. pk_sorted = sorted(pk, key=lambda j: -offs[j]) # outermost first groups, cur = [], [pk_sorted[0]] for j in pk_sorted[1:]: if offs[cur[-1]] - offs[j] <= 30: cur.append(j) else: groups.append(cur); cur = [j] groups.append(cur) top_ = max(prof[j] for j in pk) grp = next(g_ for g_ in groups if max(prof[j] for j in g_) >= 0.5 * top_) j0 = min(grp, key=lambda j: offs[j]) # innermost of that group ang0 = math.radians(angs[S[:, j0].argmax()]) strength = float(prof[j0]) # A cover edge is often a thin dark line (the paper's cut edge) or a step # into a shadow: two transitions a few px apart. Average the SIGNED # profile along the line and take the innermost transition, so the crop # starts at the paper, never on the line itself or the lid beyond it. dsig = d.sum(axis=2) if d.ndim == 3 else d sh = (ts - tc) * math.tan(ang0) Msig = np.empty_like(dsig) for i in range(len(ts)): Msig[i] = np.interp(offs + sh[i], offs, dsig[i], left=0, right=0) ps = np.median(Msig, axis=0) o0 = offs[j0] w = [j for j in range(1, len(offs) - 1) if abs(offs[j] - o0) <= 6] amax = max(abs(ps[j]) for j in w) flank = [j for j in w if abs(ps[j]) >= abs(ps[j - 1]) and abs(ps[j]) > abs(ps[j + 1]) and abs(ps[j]) >= 0.35 * amax] jf = min(flank, key=lambda j: offs[j]) if flank else j0 pol = 1.0 if ps[jf] >= 0 else -1.0 o_sel = float(offs[jf]) # How far inward does the transition reach? (blurry folds, edge highlights) # Measured on the median luminance profile along the line; the crop is # moved in past it so no blended edge pixels survive. lumb = band.sum(axis=2) if band.ndim == 3 else band[..., 0] Ml = np.empty_like(lumb) for i in range(len(ts)): Ml[i] = np.interp(offs + sh[i], offs, lumb[i]) pl = np.median(Ml, axis=0) io = lambda o: int(np.clip(np.searchsorted(offs, o), 0, len(offs) - 1)) seg_i = pl[io(o_sel - 16):io(o_sel - 9)] interior = float(np.median(seg_i)) if len(seg_i) else float(pl[0]) A = float(np.max(np.abs(pl[io(o_sel):io(o_sel + 4) + 1] - interior))) reach = 0.0 for r_ in range(0, 9): seg = pl[io(o_sel - 9):io(o_sel - r_) + 1] if np.all(np.abs(seg - interior) <= 0.12 * A + 1.0): reach = float(r_) break else: reach = 8.0 edge_inset = reach + 0.5 # sub-pixel refinement around the chosen line, 1 px sampling along the side ts2 = np.arange(trim * Ls, (1 - trim) * Ls, 2.0) loc = np.arange(-3, 3.01, 0.25, dtype=np.float32) o_line = o_sel + (ts2 - tc) * math.tan(ang0) base = p0[None, :] + ts2[:, None] * td[None, :] O = o_line[:, None] + loc[None, :] xs = (base[:, 0:1] + O * n[0]).astype(np.float32) ys = (base[:, 1:2] + O * n[1]).astype(np.float32) b = cv2.remap(arr, xs, ys, cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) lum = b.sum(axis=2) if b.ndim == 3 else b sd = pol * np.gradient(lum, 0.25, axis=1) mid = len(loc) // 2 lo, hi = mid - 5, mid + 6 # +/-1.25 px k = np.argmax(sd[:, lo:hi], axis=1) + lo r = np.arange(len(ts2)) ok = (sd[r, k] > 0) & (k > 0) & (k < len(loc) - 1) y0, y1, y2 = sd[r, k - 1], sd[r, k], sd[r, np.minimum(k + 1, len(loc) - 1)] den = y0 - 2 * y1 + y2 frac = np.where(den != 0, 0.5 * (y0 - y2) / np.where(den != 0, den, 1), 0.0) oo = o_line + loc[k] + np.clip(frac, -0.5, 0.5) * 0.25 P = (base + oo[:, None] * n[None, :])[ok].astype(np.float32) if len(P) < 30: return None keep = np.ones(len(P), bool) for _ in range(4): vx, vy, x0, y0 = cv2.fitLine(P[keep], cv2.DIST_HUBER, 0, 0.01, 0.01).ravel() dvec = np.array([vx, vy]); pp = np.array([x0, y0]) resid = np.abs((P - pp) @ np.array([-vy, vx])) new = resid < max(0.75, 3.0 * np.median(resid[keep])) if (new == keep).all(): break keep = new if dvec @ td < 0: dvec = -dvec return pp, dvec, {"n": int(keep.sum()), "rms": float(np.sqrt(np.mean(resid[keep] ** 2))), "strength": strength, "inset": edge_inset, "o_sel": o_sel, "peaks": [(int(offs[j]), round(float(prof[j]), 1)) for j in pk]} SOFT_STRENGTH = 6.0 # below this, a side has no crisp line to measure def locate_soft_edge(arr, p0, p1, n, dvec, inner=150, outer=60, trim=0.08, tol=4.0): """Place a soft side (fold, lifted or curled paper, worn edge) whose angle is taken from another edge (dvec). Walking inward from the lid, skip the lid and the blurred transition, and stop at the first stretch where the cover's content is steady along the whole side. Everything outside that point is lid, shadow or out-of-focus paper.""" Ls = float(np.linalg.norm(p1 - p0)); td = (p1 - p0) / Ls ts = np.arange(trim * Ls, (1 - trim) * Ls, 4.0); tc = ts.mean() offs = np.arange(-inner, outer + 1, 1.0, dtype=np.float32) tan_a = float(dvec @ n) / float(dvec @ td) band = _band(arr, p0, td, n, ts, offs) # (T, O, C) sh = (ts - tc) * tan_a al = np.empty_like(band) for i in range(len(ts)): for c in range(band.shape[2]): al[i, :, c] = np.interp(offs + sh[i], offs, band[i, :, c]) C = np.median(al, axis=0) # (O, C) lum = al.mean(axis=2) T = np.median(np.abs(lum - cv2.GaussianBlur(lum, (0, 0), sigmaX=0.01, sigmaY=2.0)), axis=0) # core of the soft transition: steepest smoothed change near the outline g = np.abs(np.gradient(C, axis=0)).max(axis=1) g = np.convolve(g, np.ones(7) / 7, mode="same") io = lambda o: int(np.clip(np.searchsorted(offs, o), 0, len(offs) - 1)) lo, hi = io(-75), io(25) core = lo + int(np.argmax(g[lo:hi])) # walk inward from the core until (a) the steep part of the blur is over # (change per px below half its peak) and (b) on printed art, the fine # halftone detail is back to full sharpness, i.e. the paper is on the glass. Tm = np.convolve(T, np.ones(5) / 5, mode="same") i0, i1 = max(0, core - 90), max(1, core - 45) t_int = float(np.median(Tm[i0:i1])) t_lid = float(np.median(Tm[io(outer - 25):])) use_t = t_int > 3.0 * t_lid + 1.0 g_lim = 0.5 * float(g[core]) run, need = 0, 3 o_clean = float(offs[i1]) for k in range(core, i1, -1): ok = (Tm[k] >= 0.75 * t_int) if use_t else (g[k] <= g_lim) run = run + 1 if ok else 0 if run >= need: o_clean = float(offs[k + need - 1]) - 1.0 break pp = p0 + tc * td + o_clean * n d = dvec if dvec @ td > 0 else -dvec return pp, d, {"n": 0, "rms": float("nan"), "strength": 0.0, "inset": 0.5, "o_sel": o_clean, "soft": True} def intersect(pa, da, pb, db): A = np.array([[da[0], -db[0]], [da[1], -db[1]]]) t = np.linalg.solve(A, pb - pa) return pa + t[0] * da # ------------------------------------------------------------- extraction OPP = {"left": "right", "right": "left", "top": "bottom", "bottom": "top"} ADJ = {"left": ("top", "bottom"), "right": ("top", "bottom"), "top": ("left", "right"), "bottom": ("left", "right")} def analyze_cover(arr, rect): """Fit all four edges of one cover (done once per cover).""" sides = rect_sides(rect) edges, soft = {}, [] for name, (p0, p1, n) in sides.items(): f = fit_edge(arr, p0, p1, n) if f is None or f[2]["strength"] < SOFT_STRENGTH: soft.append(name) else: edges[name] = f borrowed = {} for name in soft: p0, p1, n = sides[name] td = (p1 - p0) / np.linalg.norm(p1 - p0) if OPP[name] in edges: dv = edges[OPP[name]][1].copy(); src = OPP[name] else: adj = [edges[k][1] for k in ADJ[name] if k in edges] if adj: dv = np.mean([np.array([-v[1], v[0]]) * (1 if np.array([-v[1], v[0]]) @ td > 0 else -1) for v in adj], axis=0) src = "/".join(k for k in ADJ[name] if k in edges) else: dv = td.copy(); src = "outline" dv = dv / np.linalg.norm(dv) edges[name] = locate_soft_edge(arr, p0, p1, n, dv) borrowed[name] = src for k in ("left", "right"): # left/right point down p, d, st = edges[k]; edges[k] = (p, d if d[1] > 0 else -d, st) for k in ("top", "bottom"): # top/bottom point right p, d, st = edges[k]; edges[k] = (p, d if d[0] > 0 else -d, st) return {"edges": edges, "soft": soft, "borrowed": borrowed} def axes(theta): """Output frame for a rotation of theta degrees (+ = clockwise lean corrected).""" t = math.radians(theta) ey = np.array([math.sin(t), math.cos(t)]) ex = np.array([math.cos(t), -math.sin(t)]) return ex, ey def edge_theta(name, d): """Rotation (deg) that would make this edge exactly vertical/horizontal.""" if name in ("left", "right"): return math.degrees(math.atan2(d[0], d[1])) return math.degrees(math.atan2(-d[1], d[0])) CORNER_LENIENCY = 0.0 # 0..1 (or a {"left":.., "right":.., "top":.., "bottom":..} dict of # those): how far each side's bound relaxes from the conservative # (most-trimmed) corner toward the other one, recovering art on # non-square inserts without a manual push. Fit by cdcrop_tune_crop.py. def frame_at(cov, theta, inset, edge_adj=None): """Largest crop rectangle at rotation theta that stays inside all edges. Soft edges have no measurable angle, so they are taken as square to the frame, anchored where their in-focus art begins. edge_adj (optional, px): a manual push on one or more sides beyond that automatic boundary - positive lets some lid back in to recover art an automatic fit had to trim because the insert itself isn't quite square (the fit is conservative: it sizes every side off whichever corner needs the most trim, which can cost real art at the other corner - CORNER_LENIENCY relaxes that by a fraction automatically); negative trims further in. Keys: "left", "right", "top", "bottom".""" ex, ey = axes(theta) E = dict(cov["edges"]) for k in cov["soft"]: p, d, st = E[k] E[k] = (p, ey.copy() if k in ("left", "right") else ex.copy(), st) tl = intersect(E["top"][0], E["top"][1], E["left"][0], E["left"][1]) tr = intersect(E["top"][0], E["top"][1], E["right"][0], E["right"][1]) bl = intersect(E["bottom"][0], E["bottom"][1], E["left"][0], E["left"][1]) br = intersect(E["bottom"][0], E["bottom"][1], E["right"][0], E["right"][1]) c = (tl + tr + bl + br) / 4 uv = lambda p: ((p - c) @ ex, (p - c) @ ey) (utl, vtl), (utr, vtr), (ubl, vbl), (ubr, vbr) = map(uv, (tl, tr, bl, br)) ins = {k: max(inset, E[k][2]["inset"]) for k in E} cl = CORNER_LENIENCY beta = (lambda k: cl) if not isinstance(cl, dict) else (lambda k: cl.get(k, 0.0)) gaps = {"left": abs(utl - ubl), "right": abs(utr - ubr), "top": abs(vtl - vtr), "bottom": abs(vbl - vbr)} u0 = max(utl, ubl) - beta("left") * gaps["left"] + ins["left"] u1 = min(utr, ubr) + beta("right") * gaps["right"] - ins["right"] v0 = max(vtl, vtr) - beta("top") * gaps["top"] + ins["top"] v1 = min(vbl, vbr) + beta("bottom") * gaps["bottom"] - ins["bottom"] adj = edge_adj or {} u0 -= adj.get("left", 0.0); u1 += adj.get("right", 0.0) v0 -= adj.get("top", 0.0); v1 += adj.get("bottom", 0.0) W = max(1, int(math.floor(u1 - u0))); H = max(1, int(math.floor(v1 - v0))) u0 += ((u1 - u0) - W) / 2; v0 += ((v1 - v0) - H) / 2 origin = c + u0 * ex + v0 * ey def dev(k): return edge_theta(k, E[k][1]) - theta return {"theta": theta, "ex": ex, "ey": ey, "origin": origin, "W": W, "H": H, "ins": ins, "corners": (tl, tr, br, bl), "corner_gaps": gaps, "crop_poly": [origin, origin + W * ex, origin + W * ex + H * ey, origin + H * ey], "edge_dev_deg": {k: dev(k) for k in E}, "edge_adj": dict(adj)} def render(arr, fr): """Rotate + crop in a single Lanczos resample, rounding once at the end.""" ex, ey, o = fr["ex"], fr["ey"], fr["origin"] M = np.array([[ex[0], ey[0], o[0]], [ex[1], ey[1], o[1]]], dtype=np.float64) out = cv2.warpAffine(arr, M, (fr["W"], fr["H"]), flags=cv2.INTER_LANCZOS4 | cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_REPLICATE) return np.clip(np.rint(out), 0, 255).astype(np.uint8) def extract(arr, rect, align, inset, raw_inset=None): """Fixed-edge crop (used for --align-override and diagnostics).""" cov = analyze_cover(arr, rect) src = cov["borrowed"].get(align, align) theta = edge_theta(src, cov["edges"][src][1]) if align in cov["soft"] and src not in ("left", "right", "top", "bottom"): theta = 0.0 fr = frame_at(cov, theta, inset) if raw_inset is not None: cov2 = {**cov, "edges": {k: (p, d, {**st, "inset": raw_inset}) for k, (p, d, st) in cov["edges"].items()}} fr = frame_at(cov2, theta, raw_inset) info = {"rotation_deg": theta, "size": (fr["W"], fr["H"]), "edge_dev_deg": fr["edge_dev_deg"], "edge_fit": {k: v[2] for k, v in cov["edges"].items()}, "insets": fr["ins"], "soft": cov["soft"], "borrowed": cov["borrowed"], "used": src, "corners": fr["corners"], "crop_poly": fr["crop_poly"]} return render(arr, fr), info # ----------------------------------------------------- art-aware angle choice # # Two things decide the rotation, the same two you'd judge by eye: # * lines in the art that are meant to be square to the cover (boxes, text # baselines, borders) should come out exactly horizontal/vertical; # * the thin wedges every rotation trims off should cost as little # *interesting* art as possible (plain paper is cheap, drawing/text is not). LINE_MIN_LEN = 120 # px at full res; shorter segments are too imprecise LINE_MAX_DEV = 0.8 # deg; lines tilted more than this are deliberate TAPER_CAP = 3.0 # px; a line off by more than this is treated as deliberate H_NEAR = 0.03 # horizontal lines count only this close (fraction of height) # to the top/bottom edge, e.g. a border or strip W_V = 2.5 # 1 px of lean on vertical art lines = this many px-strips of art # Calibration (Alex's picks), all reproduced for W_V 2.0-2.75 with DEEP_BLEND 0.5: # 447_1 Dirty Pretty Things ~0.00 (box sides square + top-right sketch kept) # 447_2 Jesus Jones +0.41 left edge (lettering at top > blurry photo) # 448_2 DJ C ~+1.65 right/bottom edges (not the torn top-right corner) # 449_2 DFC left edge by a slim margin (treated as either) # 451_1 Simple Minds no preference W_H = 1.25 # ... on horizontal lines along the top/bottom edge SAL_FLOOR = 60 # percentile of cover detail treated as "plain" PROBE_DEPTHS = (30, 70) # px inside the edge where a wedge's art is judged DEEP_BLEND = 0.5 # how much the deeper probe can veto edge-only detail (wear, tears) ART_BASE = 0.0 # value of plain art relative to average detail SAL_POWER = 2.0 # >1 makes bold detail count much more than faint marks W_LEFT = 0.1 # ... and of straying from the left edge (your default) W_EDGE = 0.0 # ... and of straying from the edges' consensus (their median angle) W_BLUR = 0.0 # ... and of keeping out-of-focus art along an edge (lifted paper) BLUR_PROBE = 60 # px further in where the art's normal sharpness is measured BLUR_SLACK = 0.15 # sharpness dips smaller than this are ignored REVIEW_MARGIN = 1.0 # score gap under which a distinct alternative is flagged for review ALT_MIN_SEP = 0.1 # deg; alternatives closer than this look the same LEFT_REVIEW = 0.15 # deg; straying further than this from the left edge gets a review def _inside(quad, pts, margin): q = np.array(quad, np.float32) return np.array([cv2.pointPolygonTest(q, (float(x), float(y)), True) >= margin for x, y in pts]) def art_lines(gray, quad, theta0, side): """Straight, near-axis lines inside the art. Returns (theta_i, weight_i, segs).""" q = np.array(quad, np.float32) x0, y0 = np.floor(q.min(axis=0)).astype(int); x1, y1 = np.ceil(q.max(axis=0)).astype(int) x0, y0 = max(x0, 0), max(y0, 0) sub = gray[y0:y1, x0:x1] half = cv2.resize(sub, (sub.shape[1] // 2, sub.shape[0] // 2), interpolation=cv2.INTER_AREA) lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD) segs = lsd.detect(np.clip(half, 0, 255).astype(np.uint8))[0] if segs is None: return np.zeros(0), np.zeros(0), [] segs = segs.reshape(-1, 4) * 2 + [x0, y0, x0, y0] blur = cv2.GaussianBlur(gray, (0, 0), 1.2) th, wt, kept = [], [], [] loc = np.arange(-4, 4.01, 0.25, dtype=np.float32) for xa, ya, xb, yb in segs: P, Q = np.array([xa, ya]), np.array([xb, yb]) L = float(np.linalg.norm(Q - P)) if L < LINE_MIN_LEN: continue d = (Q - P) / L vert = abs(d[1]) > abs(d[0]) if vert and d[1] < 0: d = -d if not vert and d[0] < 0: d = -d t_s = math.degrees(math.atan2(d[0], d[1])) if vert else math.degrees(math.atan2(-d[1], d[0])) if abs(t_s - theta0) > LINE_MAX_DEV: continue if not _inside(quad, [P, Q], 40).all(): continue # sub-pixel refinement at full resolution nrm = np.array([-d[1], d[0]]) ts = np.arange(0.05 * L, 0.95 * L, 2.0) base = P[None, :] + ts[:, None] * d[None, :] xs = (base[:, 0:1] + loc[None, :] * nrm[0]).astype(np.float32) ys = (base[:, 1:2] + loc[None, :] * nrm[1]).astype(np.float32) prof = cv2.remap(blur, xs, ys, cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) g = np.gradient(prof, 0.25, axis=1) sgn = 1.0 if np.median(g[:, len(loc) // 2]) >= 0 else -1.0 g = g * sgn k = np.argmax(g[:, 4:-4], axis=1) + 4 r = np.arange(len(ts)) ok = g[r, k] > 0 y_0, y_1, y_2 = g[r, k - 1], g[r, k], g[r, k + 1] den = y_0 - 2 * y_1 + y_2 fr = np.where(den != 0, 0.5 * (y_0 - y_2) / np.where(den != 0, den, 1), 0) off = loc[k] + np.clip(fr, -0.5, 0.5) * 0.25 pts = (base + off[:, None] * nrm[None, :])[ok].astype(np.float32) if len(pts) < 0.6 * len(ts) or len(pts) < 20: continue vx, vy, px, py = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01).ravel() res = np.abs((pts - [px, py]) @ np.array([-vy, vx])) rms = float(np.sqrt(np.mean(np.minimum(res, 3) ** 2))) if rms > 1.0: continue # wobbly (hand-drawn) - not a reference line dd = np.array([vx, vy]) if vert and dd[1] < 0: dd = -dd if not vert and dd[0] < 0: dd = -dd t_f = math.degrees(math.atan2(dd[0], dd[1])) if vert else math.degrees(math.atan2(-dd[1], dd[0])) if abs(t_f - theta0) > LINE_MAX_DEV: continue contrast = float(np.median(g[r, k][ok])) C = np.array([px, py]) + (((P + Q) / 2 - [px, py]) @ dd) * dd # refined centre th.append(t_f); wt.append(L * min(1.0, contrast / 20.0)) kept.append((P, Q, vert, L, C, dd)) return merge_lines(np.array(th), np.array(wt), kept) def merge_lines(th, wt, segs, max_dang=0.2, max_off=2.5): """Join pieces of the same printed line (a divider or border broken up by detail) into one line, so its lean is judged over its full length - the way the eye sees it - rather than piece by piece.""" if len(th) == 0: return th, wt, segs used = np.zeros(len(th), bool) out_t, out_w, out_s = [], [], [] order = np.argsort(-wt) for i in order: if used[i]: continue Pi, Qi, vi, Li, Ci, di = segs[i] ni = np.array([-di[1], di[0]]) grp = [i]; used[i] = True for j in order: if used[j] or segs[j][2] != vi or abs(th[j] - th[i]) > max_dang: continue if abs((segs[j][4] - Ci) @ ni) <= max_off: grp.append(j); used[j] = True if len(grp) == 1: out_t.append(th[i]); out_w.append(wt[i]); out_s.append((Pi, Qi, vi, Li)); continue pts = [] for j in grp: _, _, _, Lj, Cj, dj = segs[j] pts += [Cj - dj * Lj / 2, Cj, Cj + dj * Lj / 2] pts = np.array(pts, np.float32) vx, vy, x0, y0 = cv2.fitLine(pts, cv2.DIST_L2, 0, 0.01, 0.01).ravel() d = np.array([vx, vy]) if vi and d[1] < 0: d = -d if not vi and d[0] < 0: d = -d t = math.degrees(math.atan2(d[0], d[1])) if vi else math.degrees(math.atan2(-d[1], d[0])) proj = (pts - [x0, y0]) @ d ext = float(proj.max() - proj.min()) P = np.array([x0, y0]) + proj.min() * d; Q = np.array([x0, y0]) + proj.max() * d out_t.append(t); out_w.append(float(sum(wt[j] for j in grp))); out_s.append((P, Q, vi, ext)) return np.array(out_t), np.array(out_w), out_s def _family_cost(thetas, th, w, L): """Consensus within one family of lines: find the angle most of the line length agrees on, drop lines that disagree with it (deliberately tilted elements), then score every angle by the RMS taper (px) of the rest.""" if len(th) == 0: return np.zeros(len(thetas)), 0.0, None taper = np.abs(th[None, :] - thetas[:, None]) * math.pi / 180 * L[None, :] K = (w[None, :] * np.exp(-taper ** 2)).sum(axis=1) i = int(np.argmax(K)) agree = taper[i] <= TAPER_CAP / 2 wa = w[agree] cost = np.sqrt((wa[None, :] * np.minimum(taper[:, agree], 8.0) ** 2).sum(axis=1) / wa.sum()) conf = min(1.0, float(wa.sum()) / 300.0) * math.sqrt(float(wa.sum()) / float(w.sum())) return cost, conf, float(thetas[i]) def line_profile(thetas, th, wt, segs, fr0): """Line-straightness cost per candidate angle, following your priorities: vertical lines in the art come first (judged against the sides, like your left-edge default); horizontal lines only matter when they run along the top or bottom edge. Returns (cost, conf_v, art_theta_v, cost_h, conf_h, art_theta_h).""" H = float(fr0["H"]); ey, o = fr0["ey"], fr0["origin"] vert = np.array([s_[2] for s_ in segs], bool) if segs else np.zeros(0, bool) L = np.array([s_[3] for s_ in segs]) if segs else np.zeros(0) near = np.zeros(len(segs), bool) for i, (P, Q, v, _) in enumerate(segs): if not v: m = float(((P + Q) / 2 - o) @ ey) near[i] = min(m, H - m) / H <= H_NEAR # Lines confirmed by a perpendicular partner at the same angle form a # frame, box or border - far stronger evidence than lone strokes, which # are often lettering or collage set at deliberately varied angles. wt = wt.copy() if len(th): base_w = wt.copy() for i in range(len(th)): other = (vert != vert[i]) & (np.abs(th - th[i]) <= 0.1) wt[i] = base_w[i] * (1.0 + 2.0 * min(1.0, float(base_w[other].sum()) / 300.0)) cv, conf_v, tv = _family_cost(thetas, th[vert], wt[vert], L[vert]) if len(th) else (np.zeros(len(thetas)), 0.0, None) hs = (~vert) & near ch, conf_h, thh = _family_cost(thetas, th[hs], wt[hs], L[hs]) if len(th) else (np.zeros(len(thetas)), 0.0, None) return cv, conf_v, tv, ch, conf_h, thh def detail_samples(gray, quad, band=45, probe=30, step=2): """Sample points in the strip along the cover's outline (where different rotations crop differently), each valued by how interesting the art is just inside it. Probing ~30 px inward skips the cut edge, creases and shadows, and tells plain background (cheap to lose) from drawing or text (expensive). Detail is measured at ~75 dpi so halftone dots and paper grain don't count as detail.""" q = np.array(quad, np.float32) x0, y0 = np.floor(q.min(axis=0)).astype(int) - 8; x1, y1 = np.ceil(q.max(axis=0)).astype(int) + 8 x0, y0 = max(x0, 0), max(y0, 0) sub = gray[y0:y1, x0:x1] f = 8 sm = cv2.resize(sub, (sub.shape[1] // f, sub.shape[0] // f), interpolation=cv2.INTER_AREA) gm = np.hypot(cv2.Sobel(sm, cv2.CV_32F, 1, 0, ksize=3), cv2.Sobel(sm, cv2.CV_32F, 0, 1, ksize=3)) / 8 I = cv2.blur(gm, (3, 3)) mask = np.zeros(sm.shape, np.uint8) cv2.fillConvexPoly(mask, ((q - [x0, y0]) / f).astype(np.int32), 1) mask = cv2.erode(mask, np.ones((5, 5), np.uint8)) # salience: faint smudges and grain count little, bold strokes/text a lot floor = float(np.percentile(I[mask > 0], SAL_FLOOR)) I = np.maximum(I - floor, 0) ** SAL_POWER mean_I = float(I[mask > 0].mean()) + 1e-3 # fine grid of points in the outline strip yy, xx = np.mgrid[y0:y1:step, x0:x1:step].astype(np.float32) P = np.stack([xx.ravel(), yy.ravel()], 1) c = q.mean(axis=0) dist = np.full(len(P), np.inf); nin = np.zeros_like(P) for i in range(4): a_, b_ = q[i], q[(i + 1) % 4] t_ = (b_ - a_) / np.linalg.norm(b_ - a_); nn = np.array([-t_[1], t_[0]], np.float32) if (c - a_) @ nn < 0: nn = -nn d_ = (P - a_) @ nn closer = d_ < dist dist = np.where(closer, d_, dist); nin[closer] = nn sel = (dist >= 0) & (dist <= band) P, nin = P[sel], nin[sel] # Probe at two depths and keep the lesser: real art (lettering, drawing, # borders) carries on into the cover, while wear, tears and fibres at a # damaged corner sit right at the edge and shouldn't be "saved". from scipy.ndimage import map_coordinates # out-of-focus art (paper lifted off the glass) counts as background: compare # halftone-scale sharpness at each point with the art ~60 px further in gs = gray[y0:y1, x0:x1].astype(np.float32) g1 = cv2.GaussianBlur(gs, (0, 0), 1.0) fine = cv2.blur(np.abs(gs - g1), (15, 15)) # halftone-scale detail mid = cv2.blur(np.abs(g1 - cv2.GaussianBlur(gs, (0, 0), 4.0)), (15, 15)) ratio = fine / np.maximum(mid, 0.5) # drops where art is out of focus, whatever its contrast at = lambda pts, im: map_coordinates(im, [pts[:, 1] - y0, pts[:, 0] - x0], order=1, mode="nearest") inner = P + BLUR_PROBE * nin r_here, r_in = at(P, ratio), at(inner, ratio) structured = (at(inner, mid) > 1.5) & (at(P, mid) > 0.8) blur = np.where(structured, np.clip(1 - r_here / np.maximum(r_in, 1e-3) - BLUR_SLACK, 0, 1), 0.0) blur[dist[sel] < 4] = 0.0 # the cut edge itself isn't "art" val = None for depth in PROBE_DEPTHS: pp_ = P + depth * nin px = ((pp_[:, 0] - x0) - (f - 1) / 2) / f; py = ((pp_[:, 1] - y0) - (f - 1) / 2) / f v_ = map_coordinates(I, [py, px], order=1, mode="constant", cval=0.0) val = v_ if val is None else (1 - DEEP_BLEND) * val + DEEP_BLEND * np.minimum(val, v_) val = val + ART_BASE * mean_I # any art is worth something, even flat colour val = val * (1 - blur) # blurry art is no loss to trim return P[:, 0], P[:, 1], val * step * step, mean_I, blur * step * step def lost_art(fr, X, Y, Iw, mean_I, side): """Interesting art inside the cover but outside this crop, expressed as the px-width of a strip of average art along one side.""" d = np.stack([X, Y], 1) - fr["origin"] u = d @ fr["ex"]; v = d @ fr["ey"] out = (u < 0) | (u > fr["W"]) | (v < 0) | (v > fr["H"]) return float(Iw[out].sum()) / (mean_I * side) def angle_components(gray, cov, inset, step=0.002): """Everything the automatic angle choice weighs, on a fine grid of angles. Computed once per cover; the weights are applied in combine_angle(), so a tuning run can try hundreds of weightings in no time.""" E = cov["edges"] named = {} for k in ("left", "right", "top", "bottom"): if k not in cov["soft"]: named[k] = edge_theta(k, E[k][1]) if not named: named["outline"] = 0.0 theta0 = float(np.median(list(named.values()))) fr0 = frame_at(cov, theta0, inset) quad = fr0["corners"] side = float(max(fr0["W"], fr0["H"])) th, wt, segs = art_lines(gray, quad, theta0, side) strong = list(th[wt > 0.1 * wt.max()]) if len(th) else [] lo = min(list(named.values()) + strong) - 0.15 hi = max(list(named.values()) + strong) + 0.15 lo, hi = max(lo, theta0 - 1.5), min(hi, theta0 + 1.5) thetas = np.unique(np.concatenate([np.arange(lo, hi + 1e-9, step), np.array(list(named.values()))])) cv, conf_v, tv, chh, conf_h, thh = line_profile(thetas, th, wt, segs, fr0) frames = [frame_at(cov, t, inset) for t in thetas] comp = {"thetas": thetas, "named": dict(named), "side": side, "quad": quad, "frames": frames, "cv": cv, "conf_v": conf_v, "tv": tv, "chh": chh, "conf_h": conf_h, "thh": thh, "segs": segs, "n_lines": int(len(th)), "left_px": (np.abs(thetas - named["left"]) * math.pi / 180 * side) if "left" in named else np.zeros_like(thetas), "edge_px": np.abs(thetas - theta0) * math.pi / 180 * side} comp["loss"] = component_loss(comp, gray) return comp def component_loss(comp, gray): """Lost-art term for every angle in comp (depends on the detail settings); also stores comp["blur"]: out-of-focus edge art each angle would keep.""" X, Y, Iw, mean_I, Bw = detail_samples(gray, comp["quad"]) loss, kept = [], [] for f in comp["frames"]: d = np.stack([X, Y], 1) - f["origin"] u = d @ f["ex"]; v = d @ f["ey"] out = (u < 0) | (u > f["W"]) | (v < 0) | (v > f["H"]) loss.append(float(Iw[out].sum()) / (mean_I * comp["side"])) kept.append(float(Bw[~out].sum()) / comp["side"]) loss, kept = np.array(loss), np.array(kept) comp["blur"] = kept - kept.min() return loss - loss.min() def combine_angle(comp, W=None): """Apply the weights: pick the best angle, a runner-up for review, and an explanation table. W overrides the module's weights (used by tuning).""" g = globals() W = W or {} wv, wh = W.get("W_V", g["W_V"]), W.get("W_H", g["W_H"]) wl, we = W.get("W_LEFT", g["W_LEFT"]), W.get("W_EDGE", g["W_EDGE"]) loss = W.get("loss", comp["loss"]) thetas, named = comp["thetas"], dict(comp["named"]) wb = W.get("W_BLUR", g["W_BLUR"]) blur = W.get("blur", comp.get("blur", np.zeros_like(loss))) total = (loss + wv * comp["conf_v"] * comp["cv"] + wh * comp["conf_h"] * comp["chh"] + wl * comp["left_px"] + we * comp["edge_px"] + wb * blur) i = int(np.argmin(total)) best = float(thetas[i]) if comp["tv"] is not None and comp["conf_v"] > 0.15: named["art verticals"] = comp["tv"] if comp["thh"] is not None and comp["conf_h"] > 0.15: named["edge horizontals"] = comp["thh"] at = lambda t: int(np.argmin(np.abs(thetas - t))) table = {name: {"theta": t, "total": float(total[at(t)]) - float(total[i]), "loss": float(loss[at(t)]), "line": float(comp["cv"][at(t)])} for name, t in named.items()} # runner-up: a genuinely different option - another local minimum at least # ALT_MIN_SEP away that scores nearly as well mins = [j for j in range(1, len(thetas) - 1) if total[j] <= total[j - 1] and total[j] <= total[j + 1]] if len(thetas) > 1: mins += [j for j in (0, len(thetas) - 1) if total[j] <= total[1 if j == 0 else len(thetas) - 2]] cand = [j for j in mins if abs(thetas[j] - best) >= ALT_MIN_SEP and total[j] - total[i] < REVIEW_MARGIN] alt, alt_why = None, None if cand: alt = float(thetas[min(cand, key=lambda j: total[j])]); alt_why = "close call" elif "left" in named and abs(best - named["left"]) > LEFT_REVIEW: alt = float(named["left"]); alt_why = "left edge" # overriding your usual habit return {"theta": best, "alt": alt, "alt_why": alt_why, "conf": max(comp["conf_v"], comp["conf_h"]), "art_theta": comp["tv"], "art_theta_h": comp["thh"], "named": table, "n_lines": comp["n_lines"], "segs": comp["segs"], "best_loss": float(loss[i]), "conf_v": comp["conf_v"], "conf_h": comp["conf_h"], "best_line": float(comp["cv"][i]), "alt_gap": float(total[at(alt)] - total[i]) if alt is not None else None} def choose_angle(arr, gray, cov, inset): """Automatic rotation for one cover (see angle_components / combine_angle).""" return combine_angle(angle_components(gray, cov, inset)) # ------------------------------------------------------------------- tuning # # The weights above can be tuned to your own picks with cdcrop_tune.py, which # writes cdcrop_tuning.json next to this script; it is loaded automatically. TUNABLE = ("W_V", "W_H", "W_LEFT", "W_EDGE", "W_BLUR", "DEEP_BLEND", "SAL_POWER", "SAL_FLOOR", "CORNER_LENIENCY") TUNING_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cdcrop_tuning.json") def load_tuning(path=TUNING_PATH): if not os.path.exists(path): return None with open(path) as fh: t = json.load(fh) for k in TUNABLE: if k in t.get("weights", {}): v = t["weights"][k] globals()[k] = v if isinstance(v, dict) else type(globals()[k])(v) return t # ------------------------------------------------------------------- discs # # A disc is found by its outline like a cover, recognised by being round, and # then fitted as the circle that follows a sharp edge most consistently all # the way round, sized near a CD's standard 120 mm (the clear plastic rim and # a one-sided shadow can't fool that; the printed label isn't always centred). # The output is a square centred on the disc; rotation trims nothing, so the # only question is which way is up. DISC_CIRC = 0.90 # outline circularity above which an object is a disc DISC_MM = 120.0 DISC_MARGIN = 6 # px of lid kept around the disc edge DISC_LINE_STRONG = 10.0 # straight-text evidence strong enough to level automatically DISC_REVIEW_DEG = 15.0 # automatic turns bigger than this get a review def fit_disc(arr, c0, dpi, coarse_d=None): """Centre and radius of a disc's physical edge (sub-pixel).""" from scipy.ndimage import map_coordinates from scipy.optimize import minimize R0 = DISC_MM / 2 / 25.4 * dpi if coarse_d is not None and abs(coarse_d / 2 / R0 - 1) > 0.06: # mini CD or other size R0 = coarse_d / 2 * 0.99 x0, y0 = max(int(c0[0] - R0 - 80), 0), max(int(c0[1] - R0 - 80), 0) sub = arr[y0:int(c0[1] + R0 + 80), x0:int(c0[0] + R0 + 80)] G = None for c in range(min(3, sub.shape[2]) if sub.ndim == 3 else 1): ch = cv2.GaussianBlur((sub[..., c] if sub.ndim == 3 else sub).astype(np.float32), (0, 0), 0.8) gm = np.hypot(cv2.Sobel(ch, cv2.CV_32F, 1, 0, ksize=3), cv2.Sobel(ch, cv2.CV_32F, 0, 1, ksize=3)) / 8 G = gm if G is None else np.maximum(G, gm) G = cv2.GaussianBlur(G, (0, 0), 1.2) th = np.radians(np.arange(0, 360, 0.25)); ct, st = np.cos(th), np.sin(th) Rlo, Rhi = R0 - 8, R0 + 6 def coverage(p): cx, cy, R = p if R < Rlo or R > Rhi: return -1.0 v = map_coordinates(G, [cy - y0 + R * st, cx - x0 + R * ct], order=1, mode="nearest") return float(np.mean(np.sqrt(np.minimum(v, 40.0)))) best = None for R in np.arange(Rlo, Rhi + 0.01, 1.0): for dx in range(-14, 15, 2): for dy in range(-14, 15, 2): sc = coverage((c0[0] + dx, c0[1] + dy, R)) if best is None or sc > best[0]: best = (sc, (c0[0] + dx, c0[1] + dy, R)) res = minimize(lambda p: -coverage(p), best[1], method="Nelder-Mead", options={"xatol": 0.05, "fatol": 1e-5}) cx, cy, R = res.x return np.array([cx, cy]), float(R) def disc_frame(center, R, theta, margin=DISC_MARGIN): """Square output centred on the disc, rotated by theta.""" S = int(math.ceil(2 * (R + margin))) ex, ey = axes(theta) origin = np.asarray(center, float) - (S - 1) / 2 * (ex + ey) poly = [origin, origin + S * ex, origin + S * ex + S * ey, origin + S * ey] return {"theta": theta, "ex": ex, "ey": ey, "origin": origin, "W": S, "H": S, "ins": {}, "corners": poly, "crop_poly": poly, "edge_dev_deg": {}, "disc": True, "center": np.asarray(center, float), "R": R} def _wrap(t): return (t + 180) % 360 - 180 def disc_orientation(gray, center, R): """Rotation candidates for a disc and an automatic pick. - level: straight text/graphics made level (quarter-turn nearest as scanned) - text arc at top: the biggest arc of text around the rim, centred at the top The automatic pick levels strong straight text, else centres a text arc that is already near the top, else leaves the disc as scanned - and flags anything that isn't clear-cut.""" c = np.asarray(center, float) cand, notes = {"as scanned": 0.0}, [] # straight lines on the label x0, y0 = max(int(c[0] - R), 0), max(int(c[1] - R), 0) sub = gray[y0:int(c[1] + R), x0:int(c[0] + R)] half = cv2.resize(sub, (sub.shape[1] // 2, sub.shape[0] // 2), interpolation=cv2.INTER_AREA) segs = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD).detect(np.clip(half, 0, 255).astype(np.uint8))[0] strength = 0.0 if segs is not None: segs = segs.reshape(-1, 4) * 2 + [x0, y0, x0, y0] mid = (segs[:, :2] + segs[:, 2:]) / 2 rad = np.hypot(mid[:, 0] - c[0], mid[:, 1] - c[1]) L = np.hypot(segs[:, 2] - segs[:, 0], segs[:, 3] - segs[:, 1]) ok = (rad > 0.30 * R) & (rad < 0.93 * R) & (L > 25) d = segs[ok, 2:] - segs[ok, :2] ang = np.degrees(np.arctan2(d[:, 1], d[:, 0])) % 90 hist = np.zeros(360) for a_, w in zip(ang, L[ok]): hist[int(a_ * 4) % 360] += w hist = np.convolve(np.r_[hist[-6:], hist, hist[:6]], np.hanning(9), "same")[6:-6] k = int(np.argmax(hist)) strength = float(hist[k] / (np.median(hist) + 1e-6)) # refine: length-weighted mean of nearby segment angles sel = np.abs(((ang - k / 4.0) + 45) % 90 - 45) < 1.0 if sel.any(): w = L[ok][sel] dev = ((ang[sel] - k / 4.0) + 45) % 90 - 45 peak = k / 4.0 + float((dev * w).sum() / w.sum()) else: peak = k / 4.0 lev = _wrap(-peak) lev = ((lev + 45) % 90) - 45 # the quarter turn nearest "as scanned" if strength >= 4: cand["level text"] = lev # text arc around the rim th = np.radians(np.arange(0, 360, 0.25)); rr = np.arange(0.80 * R, 0.965 * R, 1.0, dtype=np.float32) xs = (c[0] + np.cos(th)[:, None] * rr).astype(np.float32); ys = (c[1] + np.sin(th)[:, None] * rr).astype(np.float32) P = cv2.remap(gray.astype(np.float32), xs, ys, cv2.INTER_LINEAR) hp = np.abs(P - cv2.GaussianBlur(P, (0, 0), sigmaX=6, sigmaY=6)) e = hp.mean(axis=1) e = np.convolve(np.r_[e[-40:], e, e[:40]], np.ones(41) / 41, "same")[40:-40] on = e > np.median(e) + 2.5 * np.median(np.abs(e - np.median(e))) arc = None if on.any() and not on.all(): s2 = np.r_[on, on]; best = (0, 0); i = 0 while i < len(on): if s2[i]: j = i while j < i + len(on) and s2[j]: j += 1 if j - i > best[1] - best[0]: best = (i, j) i = j else: i += 1 span = (best[1] - best[0]) * 0.25 if span >= 20: mid = ((best[0] + best[1]) / 2 * 0.25) % 360 # image angle of the arc centre arc = (mid, span) cand["rim text at top"] = _wrap(-90 - mid) # automatic pick if "level text" in cand and strength >= DISC_LINE_STRONG: auto, why = cand["level text"], "straight text levelled" elif arc and abs(cand["rim text at top"]) <= 30: auto, why = cand["rim text at top"], "text arc centred at the top" else: auto, why = 0.0, "left as scanned (no clear straight text)" flagged = (why.startswith("left") or abs(auto) > DISC_REVIEW_DEG) return {"candidates": cand, "auto": float(auto), "why": why, "flagged": bool(flagged), "line_strength": strength} def find_objects(arr, count, dpi): """Covers and discs in one scan: [(kind, rect, circularity)], in reading order.""" rects, circ = coarse_rects(arr, count, return_shape=True) objs = [("disc" if c >= DISC_CIRC else "cover", r, c) for r, c in zip(rects, circ)] cents = np.array([o[1][0] for o in objs]) if objs else np.zeros((0, 2)) axis = 0 if len(cents) < 2 or np.ptp(cents[:, 0]) >= np.ptp(cents[:, 1]) else 1 order = np.argsort(cents[:, axis]) if len(cents) else [] return [objs[i] for i in order] # ------------------------------------------------------- scanner calibration # # A flatbed's carriage may not travel exactly perpendicular to its sensor, and # its step size along the travel may be slightly off, so a scan is the true # object seen through scan = M . true, M = [[1, k], [0, sy]] # (x = along the sensor, assumed exact; y = carriage travel). Squares come out # as slightly skewed rectangles. Measure M once with --calibrate (two scans of # the same flat cover, turned roughly a quarter turn between them); after that # every crop is computed in true geometry and the correction is folded into # the single final resample, so it costs no extra interpolation. CAL = None # {"sy": .., "k": ..} when a calibration is loaded def load_calibration(path): global CAL with open(path) as fh: CAL = json.load(fh) return CAL def true_space(arr, cal): """(arr_true, A): arr resampled into true geometry for *analysis only*, and A (2x3) mapping true coords -> scan coords, used for the final render.""" if not cal: return arr, None sy, k = float(cal["sy"]), float(cal["k"]) M = np.array([[1.0, k], [0.0, sy]]) Mi = np.linalg.inv(M) h, w = arr.shape[:2] corners = np.array([[0, 0], [w, 0], [0, h], [w, h]], float) tc = corners @ Mi.T lo = tc.min(axis=0) Wt, Ht = int(math.ceil(tc[:, 0].max() - lo[0])), int(math.ceil(tc[:, 1].max() - lo[1])) A = np.hstack([M, (M @ lo).reshape(2, 1)]) # scan = M (t + lo) arr_t = cv2.warpAffine(arr, A, (Wt, Ht), flags=cv2.INTER_LINEAR | cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_REPLICATE) return arr_t, A def frame_to_scan(fr, A): """Express an output frame defined in true geometry in scan coordinates.""" if A is None: return fr L, t = A[:, :2], A[:, 2] f2 = dict(fr) f2["origin"] = L @ np.asarray(fr["origin"], float) + t f2["ex"] = L @ np.asarray(fr["ex"], float) f2["ey"] = L @ np.asarray(fr["ey"], float) return f2 def calibrate_pair(path1, path2, verbose=True): """Measure the scanner's stretch and shear from two scans of the same flat, detailed item (a cover), turned roughly 90 degrees between scans.""" from scipy.optimize import least_squares imgs = [] for p in (path1, path2): a8, _, _ = load_scan(p) g = a8[..., :3].mean(axis=2).astype(np.float32) if a8.ndim == 3 else a8.astype(np.float32) imgs.append(g) # 1. match features on the cover at 1/2 scale, 2. fit a full affine to all # inliers (thousands of matches pin it to ~1e-6) h = 2 sm = [cv2.resize(g, (g.shape[1] // h, g.shape[0] // h), interpolation=cv2.INTER_AREA) for g in imgs] sm = [np.clip(g, 0, 255).astype(np.uint8) for g in sm] masks = [] for g in imgs: rect = coarse_rects(np.dstack([g] * 3), 1)[0] m = np.zeros(sm[0].shape[:2] if g is imgs[0] else sm[1].shape[:2], np.uint8) box = cv2.boxPoints(((rect[0][0] / h, rect[0][1] / h), (rect[1][0] / h * 0.97, rect[1][1] / h * 0.97), rect[2])) cv2.fillConvexPoly(m, box.astype(np.int32), 255) masks.append(m) det = cv2.SIFT_create(nfeatures=20000) k1, d1 = det.detectAndCompute(sm[0], masks[0]); k2, d2 = det.detectAndCompute(sm[1], masks[1]) mt = cv2.BFMatcher(cv2.NORM_L2).knnMatch(d1, d2, k=2) good = [m for m, n in (x for x in mt if len(x) == 2) if m.distance < 0.75 * n.distance] if len(good) < 50: raise RuntimeError("couldn't match the two scans - use the same flat, detailed cover in both") P1 = np.float64([k1[m.queryIdx].pt for m in good]) * h P2 = np.float64([k2[m.trainIdx].pt for m in good]) * h A0, inl = cv2.estimateAffine2D(P1, P2, method=cv2.RANSAC, ransacReprojThreshold=3.0, maxIters=5000) inl = inl.ravel().astype(bool) for _ in range(3): # least squares on inliers, then tighten X = np.hstack([P1[inl], np.ones((inl.sum(), 1))]) sol_, *_ = np.linalg.lstsq(X, P2[inl], rcond=None) A0 = sol_.T err = np.linalg.norm(P2 - (P1 @ A0[:, :2].T + A0[:, 2]), axis=1) inl = err < max(1.0, 2.5 * np.median(err[inl])) Al = A0[:, :2] cc = float(inl.sum()); rms_px = float(np.sqrt(np.mean(err[inl] ** 2))) # 3. solve Al M = M R(phi) for M = [[1,k],[0,sy]] def resid(p): k, sy, phi = p M = np.array([[1, k], [0, sy]]); Rm = np.array([[math.cos(phi), -math.sin(phi)], [math.sin(phi), math.cos(phi)]]) return (Al @ M - M @ Rm).ravel() phi0 = math.atan2(Al[1, 0] - Al[0, 1], Al[0, 0] + Al[1, 1]) sol = least_squares(resid, [0.0, 1.0, phi0]) k, sy, phi = sol.x turn = math.degrees(phi) out = {"sy": float(sy), "k": float(k), "shear_deg": math.degrees(math.atan2(k, sy)), "y_stretch_pct": (sy - 1) * 100, "turn_deg": turn, "matches": int(cc), "match_rms_px": rms_px, "fit_residual": float(np.abs(sol.fun).max()), "scans": [os.path.basename(path1), os.path.basename(path2)]} if abs(abs(turn) - 90) > 45: out["warning"] = (f"the cover was turned {turn:.0f} deg between scans; turn it roughly a quarter " f"turn for a reliable measurement") if verbose: print(f"scanner calibration from {out['scans'][0]} + {out['scans'][1]} (turned {turn:+.1f} deg):") print(f" carriage-direction stretch {out['y_stretch_pct']:+.3f}% shear {out['shear_deg']:+.4f} deg" f" ({int(cc)} matched points, {rms_px:.2f} px rms, model residual {out['fit_residual']:.1e})") if "warning" in out: print(" !! " + out["warning"]) return out def add_calibration(path, res, reset=False): """Add one measured pair to the calibration file and store the average of all pairs measured so far (each new pair refines it).""" hist = [] if not reset and os.path.exists(path): with open(path) as fh: old = json.load(fh) hist = old.get("history") or [{k: old[k] for k in ("sy", "k", "shear_deg", "y_stretch_pct", "scans") if k in old}] hist.append({k: res[k] for k in ("sy", "k", "shear_deg", "y_stretch_pct", "scans", "turn_deg") if k in res}) sy = float(np.mean([h["sy"] for h in hist])); k = float(np.mean([h["k"] for h in hist])) out = {"sy": sy, "k": k, "shear_deg": math.degrees(math.atan2(k, sy)), "y_stretch_pct": (sy - 1) * 100, "scans": [f"average of {len(hist)} pair(s)"] if len(hist) > 1 else hist[0]["scans"], "pairs": len(hist), "history": hist} if len(hist) > 1: out["spread_shear_deg"] = float(np.ptp([h["shear_deg"] for h in hist])) out["spread_stretch_pct"] = float(np.ptp([h["y_stretch_pct"] for h in hist])) with open(path, "w") as fh: json.dump(out, fh, indent=2) return out # ---------------------------------------------------------------- preview def make_preview(arr8, infos, outs, path): rgb = arr8 if arr8.ndim == 3 else np.stack([arr8] * 3, axis=2) rgb = np.ascontiguousarray(rgb[..., :3]) h, w = rgb.shape[:2] s = 1000 / max(h, w) small = cv2.resize(rgb, (int(w * s), int(h * s)), interpolation=cv2.INTER_AREA) small = cv2.cvtColor(small, cv2.COLOR_RGB2BGR) for i, inf in enumerate(infos): poly = (np.array(inf["corners"]) * s).astype(np.int32) cv2.polylines(small, [poly], True, (0, 0, 255), 1, cv2.LINE_AA) crop = (np.array(inf["crop_poly"]) * s).astype(np.int32) cv2.polylines(small, [crop], True, (0, 200, 0), 1, cv2.LINE_AA) cx, cy = crop.mean(axis=0).astype(int) cv2.putText(small, str(i + 1), (cx - 10, cy + 10), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3, cv2.LINE_AA) # corner zooms: 4x of each crop's corners tiles = [] for o in outs: o3 = o if o.ndim == 3 else np.stack([o] * 3, axis=2) o3 = cv2.cvtColor(np.ascontiguousarray(o3[..., :3]), cv2.COLOR_RGB2BGR) z = 40 cs = [o3[:z, :z], o3[:z, -z:], o3[-z:, :z], o3[-z:, -z:]] cs = [np.pad(cv2.resize(c, (z * 4, z * 4), interpolation=cv2.INTER_NEAREST), ((3, 3), (3, 3), (0, 0)), constant_values=255) for c in cs] tiles.append(np.vstack([np.hstack(cs[:2]), np.hstack(cs[2:])])) if tiles: zoom = np.hstack([np.pad(t, ((4, 4), (4, 4), (0, 0)), constant_values=128) for t in tiles]) if zoom.shape[1] > small.shape[1]: f = small.shape[1] / zoom.shape[1] zoom = cv2.resize(zoom, (small.shape[1], int(zoom.shape[0] * f)), interpolation=cv2.INTER_NEAREST) pad = small.shape[1] - zoom.shape[1] zoom = np.pad(zoom, ((0, 0), (0, pad), (0, 0)), constant_values=255) small = np.vstack([small, zoom]) cv2.imwrite(path, small, [cv2.IMWRITE_JPEG_QUALITY, 90]) # ------------------------------------------------------------------- main def describe_frame(fr, cov): return {"rotation_deg": fr["theta"], "size": (fr["W"], fr["H"]), "edge_dev_deg": fr["edge_dev_deg"], "edge_fit": {k: v[2] for k, v in cov["edges"].items()}, "insets": fr["ins"], "soft": cov["soft"], "borrowed": cov["borrowed"], "corners": fr["corners"], "crop_poly": fr["crop_poly"]} def review_sheet(arr8, frs, labels, segs, path): """A/B sheet for a close call. For each candidate: a small overview, the key art line at 2x with an exactly vertical/horizontal guide (cyan) so any tilt is obvious, and the four corners at 1:1 - the places where the two crops actually differ.""" a = arr8.astype(np.float32) key = None if segs: vs = [s_ for s_ in segs if s_[2]] or list(segs) key = max(vs, key=lambda s_: s_[3]) cols = [] for fr, lab in zip(frs, labels): o = render(a, fr) bgr = lambda im: cv2.cvtColor(np.ascontiguousarray(im), cv2.COLOR_RGB2BGR) ov = bgr(cv2.resize(o, (520, int(520 * fr["H"] / fr["W"])), interpolation=cv2.INTER_AREA)) parts = [ov] if key is not None: P, Q, vert, L = key uv = lambda p_: np.array([(p_ - fr["origin"]) @ fr["ex"], (p_ - fr["origin"]) @ fr["ey"]]) p_, q_ = uv(P), uv(Q) if vert: e0, e1 = (p_, q_) if p_[1] < q_[1] else (q_, p_) lean = e1[0] - e0[0] else: e0, e1 = (p_, q_) if p_[0] < q_[0] else (q_, p_) lean = e1[1] - e0[1] zs = [] for e in (e0, e1): cx, cy = int(round(e[0])), int(round(e[1])) x0, y0 = max(cx - 40, 0), max(cy - 40, 0) z = bgr(np.ascontiguousarray(o[y0:y0 + 80, x0:x0 + 80])) z = cv2.resize(z, (240, 240), interpolation=cv2.INTER_NEAREST) if vert: gx = int(round((e0[0] - x0 + 0.5) * 3)); cv2.line(z, (gx, 0), (gx, 239), (255, 255, 0), 1) else: gy = int(round((e0[1] - y0 + 0.5) * 3)); cv2.line(z, (0, gy), (239, gy), (255, 255, 0), 1) zs.append(np.pad(z, ((2, 2), (2, 2), (0, 0)), constant_values=255)) z = np.hstack(zs); z = np.pad(z, ((0, 0), (0, 520 - z.shape[1]), (0, 0)), constant_values=255) cap = np.full((26, 520, 3), 255, np.uint8) what = "top / bottom" if vert else "left / right" cv2.putText(cap, f"key art line, {what} ends 3x: leans {abs(lean):.1f} px over {L:.0f} px", (6, 18), cv2.FONT_HERSHEY_SIMPLEX, 0.47, (0, 0, 0), 1, cv2.LINE_AA) parts += [cap, z] n = 256 cs = [o[:n, :n], o[:n, -n:], o[-n:, :n], o[-n:, -n:]] cs = [np.pad(bgr(c), ((2, 2), (2, 2), (0, 0)), constant_values=255) for c in cs] grid = np.vstack([np.hstack(cs[:2]), np.hstack(cs[2:])]) grid = cv2.resize(grid, (520, 520), interpolation=cv2.INTER_AREA) cap = np.full((26, 520, 3), 255, np.uint8) cv2.putText(cap, "corners, 1:1 (top-left, top-right / bottom-left, bottom-right)", (6, 18), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 0), 1, cv2.LINE_AA) parts += [cap, grid] head = np.full((40, 520, 3), 255, np.uint8) cv2.putText(head, lab, (6, 28), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2, cv2.LINE_AA) cols.append(np.vstack([head] + parts)) h = max(c.shape[0] for c in cols) cols = [np.pad(c, ((0, h - c.shape[0]), (10, 10), (0, 0)), constant_values=255) for c in cols] cv2.imwrite(path, np.hstack(cols), [cv2.IMWRITE_JPEG_QUALITY, 92]) LOG_FIELDS = ["file", "cover", "chosen_deg", "mode", "alt_deg", "flagged", "line_conf_v", "line_conf_h", "art_vertical_deg", "edge_horizontal_deg", "left_deg", "right_deg", "top_deg", "bottom_deg", "soft_edges", "width", "height"] def process(path, outdir, align, overrides, count, inset, preview, log_rows): arr8, mode, dpi = load_scan(path) scan_px = arr8.astype(np.float32) # all measuring happens in true geometry; the final render maps back to the # original scan pixels, so the calibration costs no extra resample arr, CALA = true_space(scan_px, CAL) arr8v = np.clip(np.rint(arr), 0, 255).astype(np.uint8) if CALA is not None else arr8 gray = arr[..., :3].mean(axis=2) if arr.ndim == 3 else arr rects = coarse_rects(arr, count) base = os.path.splitext(os.path.basename(path))[0] if len(rects) < count: print(f"!! {base}: found {len(rects)} cover(s), expected {count}") cents = np.array([r[0] for r in rects]) if rects else np.zeros((0, 2)) axis = 0 if len(cents) < 2 or np.ptp(cents[:, 0]) >= np.ptp(cents[:, 1]) else 1 order = np.argsort(cents[:, axis]) if len(cents) else [] infos, outs = [], [] kinds = {i: "cover" for i in range(len(rects))} if rects: _, circ = coarse_rects(arr, count, return_shape=True) kinds = {i: ("disc" if c >= DISC_CIRC else "cover") for i, c in enumerate(circ)} for idx, ri in enumerate(order, start=1): if kinds.get(ri) == "disc": (cx0, cy0), (w0, h0), _ = rects[ri] center, R = fit_disc(arr, (cx0, cy0), (dpi or (600, 600))[0], (w0 + h0) / 2) ori = disc_orientation(gray, center, R) ov = overrides.get((os.path.basename(path), idx)) theta = float(ov) if ov not in (None, "auto") and ov not in ("left", "right", "top", "bottom") else ori["auto"] fr = disc_frame(center, R, theta) out = render(scan_px, frame_to_scan(fr, CALA)) save_png(out, mode, dpi, os.path.join(outdir, f"{base}_{idx}.png")) infos.append({"corners": fr["corners"], "crop_poly": fr["crop_poly"]}); outs.append(out) print(f" {base}_{idx}.png disc {2 * R / (dpi or (600,))[0] * 25.4:.1f} mm {fr['W']}x{fr['H']} " f"rotated {theta:+.2f}° ({ori['why']})") if ori["flagged"]: print(" ^ orientation needs a look: candidates " + ", ".join(f"{k} {v:+.1f}°" for k, v in ori["candidates"].items())) log_rows.append({"file": os.path.basename(path), "cover": idx, "chosen_deg": f"{theta:.4f}", "mode": "disc: " + ori["why"], "alt_deg": "", "flagged": int(ori["flagged"]), "line_conf_v": "", "line_conf_h": "", "art_vertical_deg": "", "edge_horizontal_deg": "", "left_deg": "", "right_deg": "", "top_deg": "", "bottom_deg": "", "soft_edges": "", "width": fr["W"], "height": fr["H"]}) continue cov = analyze_cover(arr, rects[ri]) ov = overrides.get((os.path.basename(path), idx), align) ch = None if ov == "auto": ch = choose_angle(arr, gray, cov, inset) theta, how = ch["theta"], "auto" elif ov in ("left", "right", "top", "bottom"): src = cov["borrowed"].get(ov, ov) theta = edge_theta(src, cov["edges"][src][1]) if src in cov["edges"] else 0.0 how = f"{ov} edge" + (f" (soft, used {src})" if ov in cov["soft"] else "") else: theta, how = float(ov), "fixed angle" fr = frame_at(cov, theta, inset) out = render(scan_px, frame_to_scan(fr, CALA)) out_path = os.path.join(outdir, f"{base}_{idx}.png") save_png(out, mode, dpi, out_path) info = describe_frame(fr, cov) infos.append(info); outs.append(out) named = {k: edge_theta(k, cov["edges"][k][1]) for k in ("left", "right", "top", "bottom") if k not in cov["soft"]} print(f" {base}_{idx}.png {fr['W']}x{fr['H']} rotated {theta:+.3f}° ({how})") print(" edge angles: " + ", ".join( f"{k} {'soft' if k in cov['soft'] else f'{named[k]:+.3f}°'}" for k in ("left", "right", "top", "bottom"))) flagged = False if ch is not None: bits = [] if ch["art_theta"] is not None and ch["conf_v"] > 0.15: bits.append(f"art verticals {ch['art_theta']:+.3f}° (confidence {ch['conf_v']:.0%})") if ch["art_theta_h"] is not None and ch["conf_h"] > 0.15: bits.append(f"edge horizontals {ch['art_theta_h']:+.3f}° (confidence {ch['conf_h']:.0%})") print(" art lines: " + ("; ".join(bits) if bits else "none usable - chose by edges and art kept")) if ch["alt"] is not None: flagged = True rdir = os.path.join(outdir, "_review"); os.makedirs(rdir, exist_ok=True) fa = frame_at(cov, ch["alt"], inset) save_png(render(arr, fa), mode, dpi, os.path.join(rdir, f"{base}_{idx}_alt.png")) bl = "left edge" if ch["alt_why"] == "left edge" else "runner-up" review_sheet(arr8, [fr, fa], [f"A (used): {theta:+.3f} deg", f"B ({bl}): {ch['alt']:+.3f} deg"], ch["segs"], os.path.join(rdir, f"{base}_{idx}_A-vs-B.jpg")) if ch["alt_why"] == "left edge": driver = ("art lines" if ch["best_line"] * W_V * ch["conf_v"] > ch["named"].get("left", {}).get("loss", 0) else "art kept near the edges") why = f"turned {theta - ch['alt']:+.2f}° from the left edge ({driver})" else: why = "close call" print(f" ^ {why} - B = {ch['alt']:+.3f}°: see _review/{base}_{idx}_A-vs-B.jpg; " f"if B looks better, use _review/{base}_{idx}_alt.png") for k in cov["soft"]: print(f" ^ {k} edge has no crisp line (fold, lifted or worn paper); " f"cropped where the in-focus art begins.") W, H = fr["W"], fr["H"] if abs(W / H - 1) > 0.02: print(f" ^ not square ({W/H:.3f}). Fine for a back inlay or digipak; on a jewel-case " f"front it usually means art blends into the lid along one edge - check the preview.") log_rows.append({ "file": os.path.basename(path), "cover": idx, "chosen_deg": f"{theta:.4f}", "mode": how, "alt_deg": "" if not ch or ch["alt"] is None else f"{ch['alt']:.4f}", "flagged": int(flagged), "line_conf_v": "" if not ch else f"{ch['conf_v']:.2f}", "line_conf_h": "" if not ch else f"{ch['conf_h']:.2f}", "art_vertical_deg": "" if not ch or ch["art_theta"] is None else f"{ch['art_theta']:.4f}", "edge_horizontal_deg": "" if not ch or ch["art_theta_h"] is None else f"{ch['art_theta_h']:.4f}", **{f"{k}_deg": (f"{named[k]:.4f}" if k in named else "soft") for k in ("left", "right", "top", "bottom")}, "soft_edges": "/".join(cov["soft"]), "width": W, "height": H}) if preview and infos: pdir = os.path.join(outdir, "_previews") os.makedirs(pdir, exist_ok=True) make_preview(arr8v, infos, outs, os.path.join(pdir, f"{base}_preview.jpg")) def _run_one(job): """Process one scan, capturing its report so parallel runs print cleanly.""" import io, contextlib, warnings global CAL CAL = job[7] job = job[:7] f = job[0] buf, rows = io.StringIO(), [] with contextlib.redirect_stdout(buf), warnings.catch_warnings(): warnings.simplefilter("ignore") print(os.path.basename(f)) try: process(*job, rows) except Exception as e: print(f"!! {f}: {e}") return buf.getvalue(), rows def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("inputs", nargs="*") ap.add_argument("-o", "--outdir", default="cropped") ap.add_argument("--align", default="auto", choices=["auto", "left", "right", "top", "bottom"], help="auto (default) weighs art lines, art kept and the left edge; " "or force one edge for every cover") ap.add_argument("--align-override", action="append", default=[], help='per cover: "scan012.bmp:2=top", "=left", "=auto" or an angle "=+0.15"') ap.add_argument("--count", type=int, default=2) ap.add_argument("--inset", type=float, default=1.0, help="minimum px trimmed inside each edge; soft edges get more automatically") ap.add_argument("--no-preview", action="store_true") ap.add_argument("--calibrate", nargs=2, metavar=("SCAN_A", "SCAN_B"), help="measure the scanner from two scans of the same flat cover turned ~90 deg, save, and exit") ap.add_argument("--calibration", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "scanner_calibration.json"), help="scanner calibration file (default: scanner_calibration.json next to this script, if present)") ap.add_argument("--no-calibration", action="store_true", help="ignore any scanner calibration") ap.add_argument("--calibrate-reset", action="store_true", help="with --calibrate: discard earlier pairs instead of averaging with them") ap.add_argument("--jobs", type=int, default=1, help="scans processed in parallel (each needs ~1.5 GB RAM at 600 dpi)") a = ap.parse_args() if a.calibrate: res = calibrate_pair(*a.calibrate) out = add_calibration(a.calibration, res, reset=a.calibrate_reset) if out["pairs"] > 1: print(f"average of {out['pairs']} pairs: stretch {out['y_stretch_pct']:+.3f}%, shear {out['shear_deg']:+.4f} deg " f"(pairs differ by up to {out['spread_stretch_pct']:.3f}% / {out['spread_shear_deg']:.4f} deg)") print(f"saved to {a.calibration} - it will be applied to every crop from now on") return if not a.no_calibration and os.path.exists(a.calibration): cal = load_calibration(a.calibration) print(f"scanner calibration: stretch {cal['y_stretch_pct']:+.3f}%, shear {cal['shear_deg']:+.4f} deg " f"(from {os.path.basename(a.calibration)})") if not a.inputs: ap.error("give scan files or a folder (or --calibrate SCAN_A SCAN_B)") overrides = {} for o in a.align_override: lhs, side = o.split("=", 1) f, n = lhs.rsplit(":", 1) overrides[(f, int(n))] = side.strip() 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(".bmp")) else: files.append(p) os.makedirs(a.outdir, exist_ok=True) log_rows = [] jobs = [(f, a.outdir, a.align, overrides, a.count, a.inset, not a.no_preview, CAL) for f in files] if a.jobs > 1 and len(files) > 1: from multiprocessing import Pool with Pool(a.jobs) as pool: for text, rows in pool.imap(_run_one, jobs): print(text, end=""); log_rows += rows else: for j in jobs: text, rows = _run_one(j) print(text, end=""); log_rows += rows if log_rows: import csv lp = os.path.join(a.outdir, "cdcrop_log.csv") new = not os.path.exists(lp) with open(lp, "a", newline="") as fh: w = csv.DictWriter(fh, fieldnames=LOG_FIELDS) if new: w.writeheader() w.writerows(log_rows) n_flag = sum(r["flagged"] for r in log_rows) print(f"\n{len(log_rows)} covers cropped, {n_flag} close call(s) in _review/. Log: {lp}") TUNING = load_tuning() if __name__ == "__main__": main()