diff --git a/modules/extras.py b/modules/extras.py index 40935f988..9a8a09761 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -65,7 +65,7 @@ def run_extras(image, gfpgan_visibility, codeformer_visibility, codeformer_weigh while len(cached_images) > 2: del cached_images[next(iter(cached_images.keys()))] - images.save_image(image, outpath, "", None, info=info, extension=opts.samples_format, short_filename=True, no_prompt=True, pnginfo_section_name="extras") + images.save_image(image, outpath, "", None, info=info, extension=opts.samples_format, short_filename=True, no_prompt=True, pnginfo_section_name="extras", p=p) return image, plaintext_to_html(info), '' diff --git a/modules/images.py b/modules/images.py index 69d149f5b..4befddf03 100644 --- a/modules/images.py +++ b/modules/images.py @@ -9,6 +9,7 @@ from fonts.ttf import Roboto import string import modules.shared +from modules import sd_samplers from modules.shared import opts LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) @@ -239,36 +240,36 @@ invalid_filename_chars = '<>:"/\\|?*\n' re_nonletters = re.compile(r'[\s'+string.punctuation+']+') -def sanitize_filename_part(text): - return text.replace(' ', '_').translate({ord(x): '' for x in invalid_filename_chars})[:128] +def sanitize_filename_part(text, replace_spaces=True): + if replace_spaces: + text = text.replace(' ', '_') + + return text.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', process_info=None): +def save_image(image, path, basename, seed=None, prompt=None, extension='png', info=None, short_filename=False, no_prompt=False, pnginfo_section_name='parameters', p=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 = opts.samples_filename_format or "[SEED]" + file_decoration = opts.samples_filename_format or "[seed]" else: - file_decoration = opts.samples_filename_format or "[SEED]-[PROMPT]" - #file_decoration = f"-{seed}-{sanitize_filename_part(prompt)[:128]}" + file_decoration = opts.samples_filename_format or "[seed]-[prompt_spaces]" - #Add new filenames tags here - file_decoration = "-" + file_decoration + file_decoration = "-" + file_decoration.lower() if seed is not None: - file_decoration = file_decoration.replace("[SEED]", str(seed)) + 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)) - + file_decoration = file_decoration.replace("[prompt]", sanitize_filename_part(prompt)[:128]) + file_decoration = file_decoration.replace("[prompt_spaces]", sanitize_filename_part(prompt, replace_spaces=False)[:128]) + if p is not None: + file_decoration = file_decoration.replace("[steps]", str(p.steps)) + file_decoration = file_decoration.replace("[cfg]", str(p.cfg_scale)) + file_decoration = file_decoration.replace("[width]", str(p.width)) + file_decoration = file_decoration.replace("[height]", str(p.height)) + file_decoration = file_decoration.replace("[sampler]", sd_samplers.samplers[p.sampler_index].name) if extension == 'png' and opts.enable_pnginfo and info is not None: pnginfo = PngImagePlugin.PngInfo() diff --git a/modules/img2img.py b/modules/img2img.py index 7461bad50..15e35093f 100644 --- a/modules/img2img.py +++ b/modules/img2img.py @@ -119,7 +119,7 @@ def img2img(prompt: str, negative_prompt: str, prompt_style: str, init_img, init grid = images.image_grid(history, batch_size, rows=1) - images.save_image(grid, p.outpath_grids, "grid", initial_seed, prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename) + images.save_image(grid, p.outpath_grids, "grid", initial_seed, prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename, p=p) processed = Processed(p, history, initial_seed, initial_info) @@ -180,7 +180,7 @@ def img2img(prompt: str, negative_prompt: str, prompt_style: str, init_img, init result_images.append(combined_image) if opts.samples_save: - images.save_image(combined_image, p.outpath_samples, "", start_seed, prompt, opts.samples_format, info=initial_info) + images.save_image(combined_image, p.outpath_samples, "", start_seed, prompt, opts.samples_format, info=initial_info, p=p) processed = Processed(p, result_images, seed, initial_info) diff --git a/modules/processing.py b/modules/processing.py index 7dc2b9afa..27445def3 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -275,7 +275,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), process_info = Processed(p, output_images, all_seeds[0], infotext())) + images.save_image(image, p.outpath_samples, "", seeds[i], prompts[i], opts.samples_format, info=infotext(n, i), p=p) output_images.append(image) @@ -291,7 +291,7 @@ def process_images(p: StableDiffusionProcessing) -> Processed: output_images.insert(0, grid) if opts.grid_save: - images.save_image(grid, p.outpath_grids, "grid", all_seeds[0], all_prompts[0], opts.grid_format, info=infotext(), short_filename=not opts.grid_extended_filename) + images.save_image(grid, p.outpath_grids, "grid", all_seeds[0], all_prompts[0], opts.grid_format, info=infotext(), short_filename=not opts.grid_extended_filename, p=p) devices.torch_gc() return Processed(p, output_images, all_seeds[0], infotext()) diff --git a/modules/shared.py b/modules/shared.py index 52d3db4b4..b651cd626 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -97,7 +97,7 @@ class Options: data = None hide_dirs = {"visible": False} if cmd_opts.hide_ui_dir_config else 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."), + "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", component_args=hide_dirs), "outdir_txt2img_samples": OptionInfo("outputs/txt2img-images", 'Output directory for txt2img images', component_args=hide_dirs), "outdir_img2img_samples": OptionInfo("outputs/img2img-images", 'Output directory for img2img images', component_args=hide_dirs), diff --git a/scripts/poor_mans_outpainting.py b/scripts/poor_mans_outpainting.py index 102aab069..b04691105 100644 --- a/scripts/poor_mans_outpainting.py +++ b/scripts/poor_mans_outpainting.py @@ -139,7 +139,7 @@ class Script(scripts.Script): combined_image = images.combine_grid(grid) if opts.samples_save: - images.save_image(combined_image, p.outpath_samples, "", initial_seed, p.prompt, opts.grid_format, info=initial_info) + images.save_image(combined_image, p.outpath_samples, "", initial_seed, p.prompt, opts.grid_format, info=initial_info, p=p) processed = Processed(p, [combined_image], initial_seed, initial_info) diff --git a/scripts/prompt_matrix.py b/scripts/prompt_matrix.py index 82096b0d4..aaece054c 100644 --- a/scripts/prompt_matrix.py +++ b/scripts/prompt_matrix.py @@ -82,6 +82,6 @@ class Script(scripts.Script): processed.images.insert(0, grid) if opts.grid_save: - images.save_image(processed.images[0], p.outpath_grids, "prompt_matrix", prompt=original_prompt, seed=processed.seed) + images.save_image(processed.images[0], p.outpath_grids, "prompt_matrix", prompt=original_prompt, seed=processed.seed, p=p) return processed diff --git a/scripts/xy_grid.py b/scripts/xy_grid.py index f511cb4a6..dd6db81cc 100644 --- a/scripts/xy_grid.py +++ b/scripts/xy_grid.py @@ -192,6 +192,6 @@ class Script(scripts.Script): ) if opts.grid_save: - images.save_image(processed.images[0], p.outpath_grids, "xy_grid", prompt=p.prompt, seed=processed.seed) + images.save_image(processed.images[0], p.outpath_grids, "xy_grid", prompt=p.prompt, seed=processed.seed, p=p) return processed