From 4535239d8a2541171efe0af4bc55476e119f205e Mon Sep 17 00:00:00 2001 From: Michoko Date: Sun, 11 Sep 2022 17:17:13 +0200 Subject: [PATCH] Add a samples filename format option Adds a "samples filename format" option in the settings. This format can be defined by tags for maximum flexibility and scalability. --- modules/images.py | 22 +++++++++++++++++++--- modules/processing.py | 2 +- modules/shared.py | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/modules/images.py b/modules/images.py index 26c399b6..69d149f5 100644 --- a/modules/images.py +++ b/modules/images.py @@ -243,16 +243,32 @@ def sanitize_filename_part(text): return text.replace(' ', '_').translate({ord(x): '' for x in invalid_filename_chars})[:128] -def save_image(image, path, basename, seed=None, prompt=None, extension='png', info=None, short_filename=False, no_prompt=False, pnginfo_section_name='parameters'): +def save_image(image, path, basename, seed=None, prompt=None, extension='png', info=None, short_filename=False, no_prompt=False, pnginfo_section_name='parameters', process_info=None): # would be better to add this as an argument in future, but will do for now is_a_grid = basename != "" if short_filename or prompt is None or seed is None: file_decoration = "" elif opts.save_to_dirs: - file_decoration = f"-{seed}" + file_decoration = opts.samples_filename_format or "[SEED]" else: - file_decoration = f"-{seed}-{sanitize_filename_part(prompt)[:128]}" + file_decoration = opts.samples_filename_format or "[SEED]-[PROMPT]" + #file_decoration = f"-{seed}-{sanitize_filename_part(prompt)[:128]}" + + #Add new filenames tags here + file_decoration = "-" + file_decoration + if seed is not None: + file_decoration = file_decoration.replace("[SEED]", str(seed)) + if prompt is not None: + file_decoration = file_decoration.replace("[PROMPT]", sanitize_filename_part(prompt)[:128]) + file_decoration = file_decoration.replace("[PROMPT_SPACES]", prompt.translate({ord(x): '' for x in invalid_filename_chars})[:128]) + if process_info is not None: + file_decoration = file_decoration.replace("[STEPS]", str(process_info.steps)) + file_decoration = file_decoration.replace("[CFG]", str(process_info.cfg_scale)) + file_decoration = file_decoration.replace("[WIDTH]", str(process_info.width)) + file_decoration = file_decoration.replace("[HEIGHT]", str(process_info.height)) + file_decoration = file_decoration.replace("[SAMPLER]", str(process_info.sampler)) + if extension == 'png' and opts.enable_pnginfo and info is not None: pnginfo = PngImagePlugin.PngInfo() diff --git a/modules/processing.py b/modules/processing.py index cf2e13d3..69e185af 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -279,7 +279,7 @@ def process_images(p: StableDiffusionProcessing) -> Processed: image = image.convert('RGB') if opts.samples_save and not p.do_not_save_samples: - images.save_image(image, p.outpath_samples, "", seeds[i], prompts[i], opts.samples_format, info=infotext(n, i)) + images.save_image(image, p.outpath_samples, "", seeds[i], prompts[i], opts.samples_format, info=infotext(n, i), process_info = Processed(p, output_images, all_seeds[0], infotext())) output_images.append(image) diff --git a/modules/shared.py b/modules/shared.py index 9eeb64e3..d828aa34 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -92,6 +92,7 @@ class Options: data = None data_labels = { + "samples_filename_format": OptionInfo("", "Samples filename format using following tags: [STEPS],[CFG],[PROMPT],[PROMPT_SPACES],[WIDTH],[HEIGHT],[SAMPLER],[SEED]. Leave blank for default."), "outdir_samples": OptionInfo("", "Output directory for images; if empty, defaults to two directories below"), "outdir_txt2img_samples": OptionInfo("outputs/txt2img-images", 'Output directory for txt2img images'), "outdir_img2img_samples": OptionInfo("outputs/img2img-images", 'Output directory for img2img images'),