Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

feat: add overwrite/increment to SaveImageTextDataSetToFolderNode (#13215)

Charles Chan6ceec29bd124
diff --git a/comfy_extras/nodes_dataset.py b/comfy_extras/nodes_dataset.py
index 35a164ec8..104d16d91 100644
--- a/comfy_extras/nodes_dataset.py
+++ b/comfy_extras/nodes_dataset.py
@@ -157,7 +157,7 @@ class LoadImageTextDataSetFromFolderNode(io.ComfyNode):
return io.NodeOutput(output_tensor, captions)
-def save_images_to_folder(image_list, output_dir, prefix="image"):
+def save_images_to_folder(image_list, output_dir, prefix="image", overwrite=True):
"""Utility function to save a list of image tensors to disk.
Args:
@@ -197,7 +197,11 @@ def save_images_to_folder(image_list, output_dir, prefix="image"):
raise ValueError(f"Expected torch.Tensor, got {type(img_tensor)}")
# Save image
- filename = f"{prefix}_{idx:05d}.png"
+ if overwrite:
+ filename = f"{prefix}_{idx:05d}.png"
+ else:
+ _, _, counter, _, resolved_prefix = folder_paths.get_save_image_path(prefix, output_dir)
+ filename = f"{resolved_prefix}_{counter:05}_{idx:05d}.png"
filepath = os.path.join(output_dir, filename)
img.save(filepath)
saved_files.append(filename)
@@ -230,19 +234,26 @@ class SaveImageDataSetToFolderNode(io.ComfyNode):
tooltip="Prefix for saved image filenames.",
advanced=True,
),
+ io.Combo.Input(
+ "mode",
+ default="overwrite",
+ options=["overwrite", "increment"],
+ tooltip="Whether to overwrite existing files or increment filenames to avoid overwriting."
+ ),
],
outputs=[],
is_deprecated=True, # This node is redundant and superseded by existing Save Image nodes where the target folder can be specified in the filename_prefix
)
@classmethod
- def execute(cls, images, folder_name, filename_prefix):
+ def execute(cls, images, folder_name, filename_prefix, mode):
# Extract scalar values
folder_name = folder_name[0]
filename_prefix = filename_prefix[0]
+ mode = mode[0]
output_dir = os.path.join(folder_paths.get_output_directory(), folder_name)
- saved_files = save_images_to_folder(images, output_dir, filename_prefix)
+ saved_files = save_images_to_folder(images, output_dir, filename_prefix, mode=='overwrite')
logging.info(f"Saved {len(saved_files)} images to {output_dir}.")
return io.NodeOutput()
@@ -278,18 +289,25 @@ class SaveImageTextDataSetToFolderNode(io.ComfyNode):
tooltip="Prefix for saved image filenames.",
advanced=True,
),
+ io.Combo.Input(
+ "mode",
+ default="overwrite",
+ options=["overwrite", "increment"],
+ tooltip="Whether to overwrite existing files or increment filenames to avoid overwriting."
+ ),
],
outputs=[],
)
@classmethod
- def execute(cls, images, folder_name, filename_prefix, texts=None):
+ def execute(cls, images, folder_name, filename_prefix, mode, texts=None):
# Extract scalar values
folder_name = folder_name[0]
filename_prefix = filename_prefix[0]
+ mode = mode[0]
output_dir = os.path.join(folder_paths.get_output_directory(), folder_name)
- saved_files = save_images_to_folder(images, output_dir, filename_prefix)
+ saved_files = save_images_to_folder(images, output_dir, filename_prefix, mode=='overwrite')
# Save captions
if texts: