Kaynağa Gözat

[BUG] Not Working As Intended - create a directory with name derived from the prompt #306

AUTOMATIC 2 yıl önce
ebeveyn
işleme
a4416f3585

+ 1 - 1
modules/extras.py

@@ -65,7 +65,7 @@ def run_extras(image, gfpgan_visibility, codeformer_visibility, codeformer_weigh
     while len(cached_images) > 2:
     while len(cached_images) > 2:
         del cached_images[next(iter(cached_images.keys()))]
         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), ''
     return image, plaintext_to_html(info), ''
 
 

+ 19 - 18
modules/images.py

@@ -9,6 +9,7 @@ from fonts.ttf import Roboto
 import string
 import string
 
 
 import modules.shared
 import modules.shared
+from modules import sd_samplers
 from modules.shared import opts
 from modules.shared import opts
 
 
 LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
 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+']+')
 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
     # would be better to add this as an argument in future, but will do for now
     is_a_grid = basename != ""
     is_a_grid = basename != ""
 
 
     if short_filename or prompt is None or seed is None:
     if short_filename or prompt is None or seed is None:
         file_decoration = ""
         file_decoration = ""
     elif opts.save_to_dirs:
     elif opts.save_to_dirs:
-        file_decoration = opts.samples_filename_format or "[SEED]"
+        file_decoration = opts.samples_filename_format or "[seed]"
     else:
     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:
     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:
     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:
     if extension == 'png' and opts.enable_pnginfo and info is not None:
         pnginfo = PngImagePlugin.PngInfo()
         pnginfo = PngImagePlugin.PngInfo()

+ 2 - 2
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)
         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)
         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)
             result_images.append(combined_image)
 
 
             if opts.samples_save:
             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)
         processed = Processed(p, result_images, seed, initial_info)
 
 

+ 2 - 2
modules/processing.py

@@ -275,7 +275,7 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
                     image = image.convert('RGB')
                     image = image.convert('RGB')
 
 
                 if opts.samples_save and not p.do_not_save_samples:
                 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)
                 output_images.append(image)
 
 
@@ -291,7 +291,7 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
                 output_images.insert(0, grid)
                 output_images.insert(0, grid)
 
 
             if opts.grid_save:
             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()
     devices.torch_gc()
     return Processed(p, output_images, all_seeds[0], infotext())
     return Processed(p, output_images, all_seeds[0], infotext())

+ 1 - 1
modules/shared.py

@@ -97,7 +97,7 @@ class Options:
     data = None
     data = None
     hide_dirs = {"visible": False} if cmd_opts.hide_ui_dir_config else None
     hide_dirs = {"visible": False} if cmd_opts.hide_ui_dir_config else None
     data_labels = {
     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_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_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),
         "outdir_img2img_samples": OptionInfo("outputs/img2img-images", 'Output directory for img2img images', component_args=hide_dirs),

+ 1 - 1
scripts/poor_mans_outpainting.py

@@ -139,7 +139,7 @@ class Script(scripts.Script):
         combined_image = images.combine_grid(grid)
         combined_image = images.combine_grid(grid)
 
 
         if opts.samples_save:
         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)
         processed = Processed(p, [combined_image], initial_seed, initial_info)
 
 

+ 1 - 1
scripts/prompt_matrix.py

@@ -82,6 +82,6 @@ class Script(scripts.Script):
         processed.images.insert(0, grid)
         processed.images.insert(0, grid)
 
 
         if opts.grid_save:
         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
         return processed

+ 1 - 1
scripts/xy_grid.py

@@ -192,6 +192,6 @@ class Script(scripts.Script):
         )
         )
 
 
         if opts.grid_save:
         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
         return processed