From 873028f783b5f8b698982f3e6393ac6232869b91 Mon Sep 17 00:00:00 2001 From: Chris Dumas Date: Thu, 15 Oct 2026 14:57:27 +0000 Subject: [PATCH] Avoid overwriting custom image names --- __init__.py | 6 +++++- dumas_image_nodes.py | 22 ++++++++++++++++++++-- tests/test_dumas_image_nodes.py | 13 +++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/__init__.py b/__init__.py index c430328..fed49b3 100644 --- a/__init__.py +++ b/__init__.py @@ -1,6 +1,7 @@ from .dumas_image_nodes import ( NODE_CLASS_MAPPINGS as IMAGE_NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS as IMAGE_NODE_DISPLAY_NAME_MAPPINGS, + _next_available_relative_path, _next_counter_for_relative_path, _folder_is_image, _list_folder_image_files, @@ -135,7 +136,10 @@ try: return 1, "" relative_template = "/".join(parts) counter = _next_counter_for_relative_path(base, relative_template) - resolved_relative = relative_template.replace("%counter%", str(counter).zfill(digits)) + if "%counter%" in relative_template: + resolved_relative = relative_template.replace("%counter%", str(counter).zfill(digits)) + else: + resolved_relative = _next_available_relative_path(base, relative_template) return counter, resolved_relative try: diff --git a/dumas_image_nodes.py b/dumas_image_nodes.py index f66861d..d70023c 100644 --- a/dumas_image_nodes.py +++ b/dumas_image_nodes.py @@ -53,6 +53,25 @@ def _next_counter_for_relative_path(base_directory, relative_template): return highest + 1 +def _next_available_relative_path(base_directory, relative_path): + """Return a collision-free relative path, adding a counter when needed.""" + relative_path = str(relative_path).replace("\\", "/") + if "%counter%" in relative_path: + counter = _next_counter_for_relative_path(base_directory, relative_path) + return relative_path.replace("%counter%", str(counter).zfill(3)) + + candidate = Path(base_directory) / Path(relative_path) + if not candidate.exists(): + return relative_path + + path = Path(relative_path) + suffix = path.suffix + name = path.name[:-len(suffix)] if suffix else path.name + counter_template = str(path.with_name(f"{name}_%counter%{suffix}")).replace("\\", "/") + counter = _next_counter_for_relative_path(base_directory, counter_template) + return counter_template.replace("%counter%", str(counter).zfill(3)) + + def _image_to_pil(image): array = image.detach().cpu().numpy() if hasattr(image, "detach") else image.cpu().numpy() array = (array[..., :3].clip(0, 1) * 255).astype("uint8") @@ -147,8 +166,7 @@ class DumasSaveImageNode: stem = stem.replace("%input%", _safe_token(name)).replace("%input2%", _safe_token(name_2)) stem = stem.replace("%width%", str(width)).replace("%height%", str(height)).replace("%batch_num%", str(index)) relative = stem.replace("\\", "/") + extension - counter = _next_counter_for_relative_path(str(base), relative) - relative = relative.replace("%counter%", str(counter).zfill(3)) + relative = _next_available_relative_path(str(base), relative) path = base / Path(relative) path.parent.mkdir(parents=True, exist_ok=True) pil = _image_to_pil(image) diff --git a/tests/test_dumas_image_nodes.py b/tests/test_dumas_image_nodes.py index 6a65fd4..12e05bb 100644 --- a/tests/test_dumas_image_nodes.py +++ b/tests/test_dumas_image_nodes.py @@ -122,6 +122,19 @@ class DumasImageNodeTests(unittest.TestCase): ["batch_003.png", "batch_004.png"], ) + def test_save_without_counter_token_does_not_overwrite(self): + with tempfile.TemporaryDirectory() as directory: + FakeImage.saved_paths.clear() + open(os.path.join(directory, "cover.png"), "wb").close() + self.nodes.DumasSaveImageNode().save_images( + [FakeBatch()], directory, "cover", "png", 95, False, True + ) + + self.assertEqual( + os.path.basename(FakeImage.saved_paths[-1][0]), + "cover_001.png", + ) + if __name__ == "__main__": unittest.main()