Skip to content

Commit 6ceec29

Browse files
authored
feat: add overwrite/increment to SaveImageTextDataSetToFolderNode (Comfy-Org#13215)
1 parent cffa2f4 commit 6ceec29

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

comfy_extras/nodes_dataset.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def execute(cls, folder):
157157
return io.NodeOutput(output_tensor, captions)
158158

159159

160-
def save_images_to_folder(image_list, output_dir, prefix="image"):
160+
def save_images_to_folder(image_list, output_dir, prefix="image", overwrite=True):
161161
"""Utility function to save a list of image tensors to disk.
162162
163163
Args:
@@ -197,7 +197,11 @@ def save_images_to_folder(image_list, output_dir, prefix="image"):
197197
raise ValueError(f"Expected torch.Tensor, got {type(img_tensor)}")
198198

199199
# Save image
200-
filename = f"{prefix}_{idx:05d}.png"
200+
if overwrite:
201+
filename = f"{prefix}_{idx:05d}.png"
202+
else:
203+
_, _, counter, _, resolved_prefix = folder_paths.get_save_image_path(prefix, output_dir)
204+
filename = f"{resolved_prefix}_{counter:05}_{idx:05d}.png"
201205
filepath = os.path.join(output_dir, filename)
202206
img.save(filepath)
203207
saved_files.append(filename)
@@ -230,19 +234,26 @@ def define_schema(cls):
230234
tooltip="Prefix for saved image filenames.",
231235
advanced=True,
232236
),
237+
io.Combo.Input(
238+
"mode",
239+
default="overwrite",
240+
options=["overwrite", "increment"],
241+
tooltip="Whether to overwrite existing files or increment filenames to avoid overwriting."
242+
),
233243
],
234244
outputs=[],
235245
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
236246
)
237247

238248
@classmethod
239-
def execute(cls, images, folder_name, filename_prefix):
249+
def execute(cls, images, folder_name, filename_prefix, mode):
240250
# Extract scalar values
241251
folder_name = folder_name[0]
242252
filename_prefix = filename_prefix[0]
253+
mode = mode[0]
243254

244255
output_dir = os.path.join(folder_paths.get_output_directory(), folder_name)
245-
saved_files = save_images_to_folder(images, output_dir, filename_prefix)
256+
saved_files = save_images_to_folder(images, output_dir, filename_prefix, mode=='overwrite')
246257

247258
logging.info(f"Saved {len(saved_files)} images to {output_dir}.")
248259
return io.NodeOutput()
@@ -278,18 +289,25 @@ def define_schema(cls):
278289
tooltip="Prefix for saved image filenames.",
279290
advanced=True,
280291
),
292+
io.Combo.Input(
293+
"mode",
294+
default="overwrite",
295+
options=["overwrite", "increment"],
296+
tooltip="Whether to overwrite existing files or increment filenames to avoid overwriting."
297+
),
281298
],
282299
outputs=[],
283300
)
284301

285302
@classmethod
286-
def execute(cls, images, folder_name, filename_prefix, texts=None):
303+
def execute(cls, images, folder_name, filename_prefix, mode, texts=None):
287304
# Extract scalar values
288305
folder_name = folder_name[0]
289306
filename_prefix = filename_prefix[0]
307+
mode = mode[0]
290308

291309
output_dir = os.path.join(folder_paths.get_output_directory(), folder_name)
292-
saved_files = save_images_to_folder(images, output_dir, filename_prefix)
310+
saved_files = save_images_to_folder(images, output_dir, filename_prefix, mode=='overwrite')
293311

294312
# Save captions
295313
if texts:

0 commit comments

Comments
 (0)