Modify list of seed for dynamic prompt and add checks for filename to prevent overwriting

This commit is contained in:
DKnight54
2025-01-29 22:43:16 +08:00
committed by GitHub
parent 7dfc5c94e2
commit 96ab0cc375

View File

@@ -2508,7 +2508,7 @@ def main(args):
metadata.add_text("crop-left", str(crop_left))
if filename is not None:
fln = filename
fln = first_available_filename(args.outdir, filename) #Checks to make sure is not existing file, else returns first available sequential filename
else:
if args.use_original_file_name and init_images is not None:
if type(init_images) is list:
@@ -2586,7 +2586,8 @@ def main(args):
negative_scale = args.negative_scale
steps = args.steps
seed = None
seeds = None
if pi == 0:
seeds = None
strength = 0.8 if args.strength is None else args.strength
negative_prompt = ""
clip_prompt = None
@@ -2670,6 +2671,8 @@ def main(args):
m = re.match(r"d ([\d,]+)", parg, re.IGNORECASE)
if m: # seed
if pi > 0 and len(raw_prompts) > 1: #Bypass od 2nd loop for dynamic prompts
continue
seeds = [int(d) for d in m.group(1).split(",")]
logger.info(f"seeds: {seeds}")
continue
@@ -2802,7 +2805,11 @@ def main(args):
logger.error(f"Exception in parsing / 解析エラー: {parg}")
logger.error(f"{ex}")
# override Deep Shrink
# override filename to add index number if more than one image per prompt
if filename is not None and (args.images_per_prompt > 1 or len(raw_prompts) > 1):
filename = filename + "_%s" % pi
# override Deep Shrink
if ds_depth_1 is not None:
if ds_depth_1 < 0:
ds_depth_1 = args.ds_depth_1 or 3
@@ -2835,12 +2842,16 @@ def main(args):
# prepare seed
if seeds is not None: # given in prompt
# num_images_per_promptが多い場合は足りなくなるので、足りない分は前のを使う
# Previous implementation may result in unexpected behaviour when number of seeds is lesss than number of repeats. Last seed is taken for rest of repeated prompts
# Previous implementation may result in unexpected behaviour when number of seeds is lesss than number of repeats. Last seed is taken for rest of repeated prompts. Add condition if last element is -1, to start randomizing seed.
if len(seeds) > 1:
seed = seeds.pop(0)
elif len(seeds) == 1:
seed = seeds.pop(0)
seeds = None
if seeds[0] == -1:
seeds = None
else:
seed = seeds.pop(0)
else:
if args.iter_same_seed:
seed = iter_seed
@@ -2940,7 +2951,35 @@ def main(args):
logger.info("done!")
def first_available_filename(path, filename):
"""
Checks if filename is in use.
if filename is in use, appends a running number
e.g. filename = 'file.png':
file.png
file_1.png
file_2.png
Runs in log(n) time where n is the number of existing files in sequence
"""
i = 1
if not os.path.exists(os.path.join(path, filename)):
return filename
fileext = os.path.splitext(filename)
filename = fileext[0] + "_%s" + fileext[1]
# First do an exponential search
while os.path.exists(os.path.join(path,filename % i)):
i = i * 2
# Result lies somewhere in the interval (i/2..i]
# We call this interval (a..b] and narrow it down until a + 1 = b
a, b = (i // 2, i)
while a + 1 < b:
c = (a + b) // 2 # interval midpoint
a, b = (c, b) if os.path.exists(os.path.join(path,filename % c)) else (a, c)
return filename % b
def setup_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()