From bd9b3d15e8f631f9475d14a5fd07560c177dc2f3 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Fri, 1 Sep 2023 04:05:58 +0900 Subject: [PATCH 1/3] fix batch img2img output dir with script --- modules/img2img.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/img2img.py b/modules/img2img.py index c81c7ab9e..0c6d1af57 100644 --- a/modules/img2img.py +++ b/modules/img2img.py @@ -114,11 +114,14 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=Fal else: p.override_settings.pop("sd_model_checkpoint", None) + if output_dir: + p.outpath_samples = output_dir + p.override_settings['save_to_dirs'] = False + proc = modules.scripts.scripts_img2img.run(p, *args) + if proc is None: if output_dir: - p.outpath_samples = output_dir - p.override_settings['save_to_dirs'] = False if p.n_iter > 1 or p.batch_size > 1: p.override_settings['samples_filename_pattern'] = f'{image_path.stem}-[generation_number]' else: From 35d1c94549cf75e7e312372d90fee0acc2806426 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 6 Sep 2023 20:24:26 +0900 Subject: [PATCH 2/3] save_images_add_number_suffix --- modules/images.py | 10 +++++++++- modules/shared_options.py | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/images.py b/modules/images.py index eb6447338..10dcd9ab8 100644 --- a/modules/images.py +++ b/modules/images.py @@ -661,7 +661,15 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i save_image_with_geninfo(image_to_save, info, temp_file_path, extension, existing_pnginfo=params.pnginfo, pnginfo_section_name=pnginfo_section_name) - os.replace(temp_file_path, filename_without_extension + extension) + full_file_name = filename_without_extension + extension + if shared.opts.save_images_add_number_suffix and os.path.exists(full_file_name): + count = 1 + while True: + full_file_name = f"{filename_without_extension}_{count}{extension}" + if not os.path.exists(full_file_name): + break + count += 1 + os.replace(temp_file_path, full_file_name) fullfn_without_extension, extension = os.path.splitext(params.filename) if hasattr(os, 'statvfs'): diff --git a/modules/shared_options.py b/modules/shared_options.py index 00b273faa..2f4caa9dd 100644 --- a/modules/shared_options.py +++ b/modules/shared_options.py @@ -26,7 +26,7 @@ options_templates.update(options_section(('saving-images', "Saving images/grids" "samples_format": OptionInfo('png', 'File format for images'), "samples_filename_pattern": OptionInfo("", "Images filename pattern", component_args=hide_dirs).link("wiki", "https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Custom-Images-Filename-Name-and-Subdirectory"), "save_images_add_number": OptionInfo(True, "Add number to filename when saving", component_args=hide_dirs), - + "save_images_add_number_suffix": OptionInfo(True, "Add number suffix when necessary", component_args=hide_dirs).info("prevent existing image from being override"), "grid_save": OptionInfo(True, "Always save all generated image grids"), "grid_format": OptionInfo('png', 'File format for grids'), "grid_extended_filename": OptionInfo(False, "Add extended info (seed, prompt) to filename when saving grid"), From 657404b75b2f214a97281afbec1adcb4313d24eb Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 6 Sep 2023 20:33:43 +0900 Subject: [PATCH 3/3] use original filename batch img2img with scripts --- modules/img2img.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/modules/img2img.py b/modules/img2img.py index 0c6d1af57..c1cae22f8 100644 --- a/modules/img2img.py +++ b/modules/img2img.py @@ -117,15 +117,14 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=Fal if output_dir: p.outpath_samples = output_dir p.override_settings['save_to_dirs'] = False + if p.n_iter > 1 or p.batch_size > 1: + p.override_settings['samples_filename_pattern'] = f'{image_path.stem}-[generation_number]' + else: + p.override_settings['samples_filename_pattern'] = f'{image_path.stem}' proc = modules.scripts.scripts_img2img.run(p, *args) if proc is None: - if output_dir: - if p.n_iter > 1 or p.batch_size > 1: - p.override_settings['samples_filename_pattern'] = f'{image_path.stem}-[generation_number]' - else: - p.override_settings['samples_filename_pattern'] = f'{image_path.stem}' process_images(p)