From 8da1bd48bf9d0411cd9ba87b8d9220743cb5807e Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Mon, 24 Oct 2022 14:03:58 +0300 Subject: [PATCH] add an option to skip adding number to filenames when saving. rework filename pattern function go through the pattern once and not calculate any of replacements until they are actually encountered in the pattern. --- modules/images.py | 236 ++++++++++++++++++++++++---------------------- modules/shared.py | 8 +- 2 files changed, 128 insertions(+), 116 deletions(-) diff --git a/modules/images.py b/modules/images.py index a9b1330d..848ede75 100644 --- a/modules/images.py +++ b/modules/images.py @@ -1,4 +1,7 @@ import datetime +import sys +import traceback + import pytz import io import math @@ -274,10 +277,15 @@ invalid_filename_chars = '<>:"/\\|?*\n' invalid_filename_prefix = ' ' invalid_filename_postfix = ' .' re_nonletters = re.compile(r'[\s' + string.punctuation + ']+') +re_pattern = re.compile(r"([^\[\]]+|\[([^]]+)]|[\[\]]*)") +re_pattern_arg = re.compile(r"(.*)<([^>]*)>$") max_filename_part_length = 128 def sanitize_filename_part(text, replace_spaces=True): + if text is None: + return None + if replace_spaces: text = text.replace(' ', '_') @@ -287,49 +295,103 @@ def sanitize_filename_part(text, replace_spaces=True): return text -def apply_filename_pattern(x, p, seed, prompt): - max_prompt_words = opts.directories_max_prompt_words +class FilenameGenerator: + replacements = { + 'seed': lambda self: self.seed if self.seed is not None else '', + 'steps': lambda self: self.p and self.p.steps, + 'cfg': lambda self: self.p and self.p.cfg_scale, + 'width': lambda self: self.p and self.p.width, + 'height': lambda self: self.p and self.p.height, + 'styles': lambda self: self.p and sanitize_filename_part(", ".join([style for style in self.p.styles if not style == "None"]) or "None", replace_spaces=False), + 'sampler': lambda self: self.p and sanitize_filename_part(sd_samplers.samplers[self.p.sampler_index].name, replace_spaces=False), + 'model_hash': lambda self: getattr(self.p, "sd_model_hash", shared.sd_model.sd_model_hash), + 'date': lambda self: datetime.datetime.now().strftime('%Y-%m-%d'), + 'datetime': lambda self, *args: self.datetime(*args), # accepts formats: [datetime], [datetime], [datetime