Prioritize older images & make use MM:SS format

This commit is contained in:
2026-03-16 14:00:35 +00:00
parent 7383209804
commit ee8fa0960d
2 changed files with 326 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ import argparse
import base64
import json
import mimetypes
import re
import sys
from pathlib import Path
@@ -130,6 +131,29 @@ def extract_rowing_data(image_path: str) -> dict:
}
_VERSION_RE = re.compile(r"^(?P<base>.+?)(?:\s+\((?P<num>\d+)\))?(?P<ext>\.\w+)$")
def _keep_latest_versions(paths: list[Path]) -> list[Path]:
"""Keep only the highest-numbered version of each base filename.
e.g. given IMG_5454_screen.jpg, IMG_5454_screen (1).jpg, IMG_5454_screen (3).jpg
only IMG_5454_screen (3).jpg is kept. No-suffix counts as version 0.
"""
best: dict[str, tuple[int, Path]] = {}
for p in paths:
m = _VERSION_RE.match(p.name)
if not m:
best.setdefault(p.name, (0, p))
continue
base_key = m.group("base") + m.group("ext")
num = int(m.group("num")) if m.group("num") else 0
prev_num, _ = best.get(base_key, (-1, p))
if num > prev_num:
best[base_key] = (num, p)
return sorted(p for _, p in best.values())
def main():
parser = argparse.ArgumentParser(description="Extract rowing data from display photos")
parser.add_argument("--image", help="Path to a single image")
@@ -145,12 +169,13 @@ def main():
if args.dir:
dir_path = Path(args.dir)
image_paths = sorted(
all_images = sorted(
p for p in dir_path.iterdir() if p.suffix.lower() in IMAGE_EXTS
)
if not image_paths:
if not all_images:
print(f"No images found in {args.dir}")
sys.exit(1)
image_paths = _keep_latest_versions(all_images)
else:
image_paths = [Path(args.image)]