|
|
|
@ -23,7 +23,9 @@ def get_image_parts(image_path:pathlib.Path) -> tuple[str, str, str]:
@@ -23,7 +23,9 @@ def get_image_parts(image_path:pathlib.Path) -> tuple[str, str, str]:
|
|
|
|
|
return image_path.stem.rsplit("_", maxsplit=2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_unique_parts(images:list[pathlib.Path]) -> list[list[str], list[str], list[str]]: |
|
|
|
|
def get_unique_parts( |
|
|
|
|
images: list[pathlib.Path], |
|
|
|
|
) -> list[list[str], list[str], list[str]]: |
|
|
|
|
parts = [get_image_parts(p) for p in images] |
|
|
|
|
return [sorted(set(items)) for items in zip(*parts)] |
|
|
|
|
|
|
|
|
@ -58,14 +60,18 @@ def read_rename_map(map_file:pathlib.Path) -> dict[str, str]:
@@ -58,14 +60,18 @@ def read_rename_map(map_file:pathlib.Path) -> dict[str, str]:
|
|
|
|
|
return {k: v for k, v in _parse_rename_map(content)} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_rename(images:list[pathlib.Path], rename_map:dict[str, str], sep="_") -> list[tuple[pathlib.Path, pathlib.Path]]: |
|
|
|
|
def prepare_rename( |
|
|
|
|
images: list[pathlib.Path], rename_map: dict[str, str], sep="_" |
|
|
|
|
) -> list[tuple[pathlib.Path, pathlib.Path]]: |
|
|
|
|
for path in images: |
|
|
|
|
renamed_parts = [rename_map[p] for p in get_image_parts(path)] |
|
|
|
|
yield path, path.with_stem(sep.join(renamed_parts)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@click.command() |
|
|
|
|
@click.argument("directory", type=click.Path(exists=True, file_okay=False, dir_okay=True)) |
|
|
|
|
@click.argument( |
|
|
|
|
"directory", type=click.Path(exists=True, file_okay=False, dir_okay=True) |
|
|
|
|
) |
|
|
|
|
def sensospot_rename(directory): |
|
|
|
|
images = list_images(directory) |
|
|
|
|
if not images: |
|
|
|
@ -85,7 +91,9 @@ def sensospot_rename(directory):
@@ -85,7 +91,9 @@ def sensospot_rename(directory):
|
|
|
|
|
try: |
|
|
|
|
prepared = list(prepare_rename(images, rename_map)) |
|
|
|
|
except Exception: |
|
|
|
|
raise click.UsageError("Could not rename images. Please check the image directory and rename map file.") |
|
|
|
|
raise click.UsageError( |
|
|
|
|
"Could not rename images. Please check the image directory and rename map file." |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
for src, dst in prepared: |
|
|
|
|
click.echo(f"renaming: {src} -> {dst}") |
|
|
|
|