Avoid overwriting custom image names

This commit is contained in:
2026-10-15 14:57:27 +00:00
parent 64a8e8a439
commit 873028f783
3 changed files with 38 additions and 3 deletions
+5 -1
View File
@@ -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:
+20 -2
View File
@@ -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)
+13
View File
@@ -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()