Propose black and white points from the scan
Levels shipped at 0–255 unless someone moved the sliders, and Bicycle Race showed what that costs. Its ink is grey, not black — a scanned engraving, ink at 2–95, paper at 163–255 — and with alpha = 255 − luminance that greyness becomes transparency. No pixel in the exported bundle was even fully opaque, and the downscale to the song's width blended every stroke edge further. Nothing downstream can rescue it. So detection proposes levels too, like it proposes cuts and skew. Notation is two-tone, which makes Otsu's split the measurement wanted; the points sit halfway from it to each end of the range, so the ramp between them survives as antialiasing rather than going jagged. A page already scanned bilevel has no interior split — Otsu degenerates to 0 — and is left alone. Per page, with the median becoming the song's, so a near-blank page cannot set them.
This commit is contained in:
@@ -15,6 +15,7 @@ import hashlib
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from statistics import median
|
||||
|
||||
from .detect import PageDetection
|
||||
|
||||
@@ -296,7 +297,18 @@ class Project:
|
||||
content_rect=detection.content,
|
||||
)
|
||||
)
|
||||
return cls(source=source, source_hash=hash_file(source), pages=pages)
|
||||
# Levels per song, not per page: a scanner's contrast does not change
|
||||
# between sheets, and one pair of sliders for the whole song is what a
|
||||
# user actually wants to nudge. The median keeps a near-blank page —
|
||||
# where the ink/paper split is guesswork — from setting them.
|
||||
proposals = [d.levels for d in detections] or [(0, 255)]
|
||||
levels = (
|
||||
int(median(b for b, _ in proposals)),
|
||||
int(median(w for _, w in proposals)),
|
||||
)
|
||||
return cls(
|
||||
source=source, source_hash=hash_file(source), pages=pages, levels=levels
|
||||
)
|
||||
|
||||
def save(self, path: Path | None = None) -> Path:
|
||||
"""Atomic write, so a crash mid-save cannot destroy the previous state."""
|
||||
|
||||
Reference in New Issue
Block a user