Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix batch img2img output dir with script #12926

Merged
merged 3 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion modules/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,15 @@ def _atomically_save_image(image_to_save, filename_without_extension, extension)

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'):
Expand Down
16 changes: 9 additions & 7 deletions modules/img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,17 @@ 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
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:
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}'
process_images(p)


Expand Down
2 changes: 1 addition & 1 deletion modules/shared_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"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"),
Expand Down