utils.py 813 B

123456789101112131415161718192021222324252627282930313233343536
  1. from __future__ import annotations
  2. from pathlib import Path
  3. import re
  4. IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".bmp", ".tif", ".tiff"}
  5. def is_image_file(path: Path) -> bool:
  6. return path.is_file() and path.suffix.lower() in IMAGE_EXTENSIONS
  7. def list_image_files(folder: Path) -> list[Path]:
  8. if not folder.exists():
  9. raise FileNotFoundError(f"Folder not found: {folder}")
  10. return sorted([path for path in folder.iterdir() if is_image_file(path)])
  11. def stem_without_mask_suffix(name: str) -> str:
  12. stem = Path(name).stem
  13. stem = re.sub(r"_mask(_\d+)?$", "", stem)
  14. return stem
  15. def relative_stem(path: Path) -> str:
  16. return path.stem
  17. __all__ = [
  18. "IMAGE_EXTENSIONS",
  19. "is_image_file",
  20. "list_image_files",
  21. "stem_without_mask_suffix",
  22. "relative_stem",
  23. ]