From 8fbabe0e41745a48e78af2a0a7154b4d239d88e9 Mon Sep 17 00:00:00 2001 From: Hay Kranen Date: Wed, 28 Sep 2022 22:55:35 +0200 Subject: [PATCH 1/2] Adding a more descriptive default output filename --- text2image.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/text2image.py b/text2image.py index 4ddeec3..a6da26b 100644 --- a/text2image.py +++ b/text2image.py @@ -1,3 +1,4 @@ +from datetime import datetime from tensorflow import keras from stable_diffusion_tf.stable_diffusion import Text2Image import argparse @@ -17,7 +18,7 @@ "--output", type=str, nargs="?", - default="output", + default=None, help="where to save the output image", ) @@ -68,6 +69,7 @@ args = parser.parse_args() + if args.mp: print("Using mixed precision.") keras.mixed_precision.set_global_policy("mixed_float16") @@ -82,12 +84,28 @@ seed=args.seed, ) -fname = args.output +if not args.output: + # When not providing an output filename, create something using + # the prompt and a timestamp to prevent overwriting of existing files + # Get a timestamp without microseconds, and replace colons with dots + timestamp = datetime.now().isoformat("T").split(".")[0].replace(":", ".") + + # Create a 'slug' with only valid alphanumeric characters and spaces to + # prevent filename issues + slug_prompt = "".join(c for c in args.prompt if (c.isalnum() or c in "_- ")) + + # And create the final filename + fname = f"{timestamp} - {slug_prompt}" +else: + # Output filename provided, use that + fname = args.output + if fname.endswith(".png"): fname = fname[:-4] + if args.batch_size == 1: - Image.fromarray(img[0]).save(args.output + ".png") - print(f"saved at {args.output}.png") + Image.fromarray(img[0]).save(f"{fname}.png") + print(f"saved at {fname}.png") else: for i in range(args.batch_size): fname_i = f"{fname}_{i}.png" From 9a68a5949620bb6f4c5457e68d13e3ebb7329a14 Mon Sep 17 00:00:00 2001 From: Hay Kranen Date: Thu, 29 Sep 2022 10:46:34 +0200 Subject: [PATCH 2/2] Trimming the length of the prompt slug to a maximum of 100 characters --- text2image.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/text2image.py b/text2image.py index a6da26b..ea66b91 100644 --- a/text2image.py +++ b/text2image.py @@ -94,6 +94,9 @@ # prevent filename issues slug_prompt = "".join(c for c in args.prompt if (c.isalnum() or c in "_- ")) + # Trim the length to 100 characters to prevent issues with maximum pathlength + slug_prompt = slug_prompt[0:100] + # And create the final filename fname = f"{timestamp} - {slug_prompt}" else: