From 8da1bd48bf9d0411cd9ba87b8d9220743cb5807e Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Mon, 24 Oct 2022 14:03:58 +0300 Subject: 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 | 250 ++++++++++++++++++++++++++++-------------------------- modules/shared.py | 8 +- 2 files changed, 135 insertions(+), 123 deletions(-) (limited to 'modules') 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 - - if seed is not None: - x = re.sub(r'\[seed]', str(seed), x, flags=re.IGNORECASE) - - if p is not None: - x = re.sub(r'\[steps]', str(p.steps), x, flags=re.IGNORECASE) - x = re.sub(r'\[cfg]', str(p.cfg_scale), x, flags=re.IGNORECASE) - x = re.sub(r'\[width]', str(p.width), x, flags=re.IGNORECASE) - x = re.sub(r'\[height]', str(p.height), x, flags=re.IGNORECASE) - x = re.sub(r'\[styles]', sanitize_filename_part(", ".join([x for x in p.styles if not x == "None"]) or "None", replace_spaces=False), x, flags=re.IGNORECASE) - x = re.sub(r'\[sampler]', sanitize_filename_part(sd_samplers.samplers[p.sampler_index].name, replace_spaces=False), x, flags=re.IGNORECASE) - - x = re.sub(r'\[model_hash]', getattr(p, "sd_model_hash", shared.sd_model.sd_model_hash), x, flags=re.IGNORECASE) - current_time = datetime.datetime.now() - x = re.sub(r'\[date]', current_time.strftime('%Y-%m-%d'), x, flags=re.IGNORECASE) - x = replace_datetime(x, current_time) # replace [datetime], [datetime], [datetime