Avoid overwriting custom image names
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from .dumas_image_nodes import (
|
from .dumas_image_nodes import (
|
||||||
NODE_CLASS_MAPPINGS as IMAGE_NODE_CLASS_MAPPINGS,
|
NODE_CLASS_MAPPINGS as IMAGE_NODE_CLASS_MAPPINGS,
|
||||||
NODE_DISPLAY_NAME_MAPPINGS as IMAGE_NODE_DISPLAY_NAME_MAPPINGS,
|
NODE_DISPLAY_NAME_MAPPINGS as IMAGE_NODE_DISPLAY_NAME_MAPPINGS,
|
||||||
|
_next_available_relative_path,
|
||||||
_next_counter_for_relative_path,
|
_next_counter_for_relative_path,
|
||||||
_folder_is_image,
|
_folder_is_image,
|
||||||
_list_folder_image_files,
|
_list_folder_image_files,
|
||||||
@@ -135,7 +136,10 @@ try:
|
|||||||
return 1, ""
|
return 1, ""
|
||||||
relative_template = "/".join(parts)
|
relative_template = "/".join(parts)
|
||||||
counter = _next_counter_for_relative_path(base, relative_template)
|
counter = _next_counter_for_relative_path(base, relative_template)
|
||||||
|
if "%counter%" in relative_template:
|
||||||
resolved_relative = relative_template.replace("%counter%", str(counter).zfill(digits))
|
resolved_relative = relative_template.replace("%counter%", str(counter).zfill(digits))
|
||||||
|
else:
|
||||||
|
resolved_relative = _next_available_relative_path(base, relative_template)
|
||||||
return counter, resolved_relative
|
return counter, resolved_relative
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
+20
-2
@@ -53,6 +53,25 @@ def _next_counter_for_relative_path(base_directory, relative_template):
|
|||||||
return highest + 1
|
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):
|
def _image_to_pil(image):
|
||||||
array = image.detach().cpu().numpy() if hasattr(image, "detach") else image.cpu().numpy()
|
array = image.detach().cpu().numpy() if hasattr(image, "detach") else image.cpu().numpy()
|
||||||
array = (array[..., :3].clip(0, 1) * 255).astype("uint8")
|
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("%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))
|
stem = stem.replace("%width%", str(width)).replace("%height%", str(height)).replace("%batch_num%", str(index))
|
||||||
relative = stem.replace("\\", "/") + extension
|
relative = stem.replace("\\", "/") + extension
|
||||||
counter = _next_counter_for_relative_path(str(base), relative)
|
relative = _next_available_relative_path(str(base), relative)
|
||||||
relative = relative.replace("%counter%", str(counter).zfill(3))
|
|
||||||
path = base / Path(relative)
|
path = base / Path(relative)
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
pil = _image_to_pil(image)
|
pil = _image_to_pil(image)
|
||||||
|
|||||||
@@ -122,6 +122,19 @@ class DumasImageNodeTests(unittest.TestCase):
|
|||||||
["batch_003.png", "batch_004.png"],
|
["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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user