Avoid overwriting custom image names
This commit is contained in:
+20
-2
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user