The pdf module is the whole of M1 except the debug overlay, which needs detection results to draw. Source type is detected per PDF by looking for a page-covering image, and is always reported for confirmation rather than applied silently (ADR 0004). Rasters are extracted via Pixmap(doc, xref) rather than by decoding extract_image() bytes, because MuPDF handles JBIG2 and CCITT scans that no image library will. Scanned pages are extracted at the embedded image's native resolution; only vector pages are rendered, at 600 DPI. Verified against the corpus: Elaman nalka (vector) renders 4959x7017, Ketun joululaulu (scan) loads 1653x2332, Engel (scan) 2552x3504 — and Engel's page 2 is 2480 wide where page 1 is 2552, so scan width varies within one PDF. tests/test_pdf.py builds its own PDFs so the check runs without corpus files, which are copyrighted and gitignored. Closes #1, #2, #3
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
"""Command line entry point."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import sys
|
|
|
|
from . import __version__
|
|
from .pdf import SourceType, open_source, page_raster
|
|
|
|
|
|
def _info(args: argparse.Namespace) -> int:
|
|
source = open_source(args.pdf, SourceType(args.type) if args.type else None)
|
|
note = f" (detected {source.detected.value}, overridden)" if source.overridden else ""
|
|
print(f"{source.path.name}: {source.type.value}{note}, {len(source)} pages")
|
|
for i in range(len(source)):
|
|
h, w = page_raster(source, i).shape
|
|
print(f" p{i + 1:<3} {w}x{h}")
|
|
source.close()
|
|
return 0
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(
|
|
prog="noteman-slicer",
|
|
description="Cut score PDFs into noteman's slice images and markers.",
|
|
)
|
|
parser.add_argument("--version", action="version", version=__version__)
|
|
sub = parser.add_subparsers(dest="command", required=True)
|
|
|
|
info = sub.add_parser("info", help="classify a PDF and report its page rasters")
|
|
info.add_argument("pdf")
|
|
info.add_argument(
|
|
"--type",
|
|
choices=[t.value for t in SourceType],
|
|
help="override source-type detection",
|
|
)
|
|
info.set_defaults(func=_info)
|
|
|
|
args = parser.parse_args(argv)
|
|
return args.func(args)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|