Skip to content

Commit

Permalink
Sanitize filename template and allow more characters
Browse files Browse the repository at this point in the history
  • Loading branch information
benrugg committed Oct 22, 2023
1 parent 8d585d2 commit 287c9a4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package_name = 'AI-Render'
default_prompt_text = 'Describe anything you can imagine'
default_image_filename_template = "ai-render-{timestamp}"
filename_template_allowed_vars = ['timestamp', 'prompt', 'width', 'height', 'seed', 'steps', 'image_similarity', 'sampler', 'negative_prompt']
tmp_path_subfolder = 'ai-render-temp'
animated_prompts_text_name = 'AI Render Animated Prompts'

Expand Down
2 changes: 1 addition & 1 deletion properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class AIRProperties(bpy.types.PropertyGroup):
image_filename_template: bpy.props.StringProperty(
name="Filename Template",
default=config.default_image_filename_template,
description="The filename template for generated images. Can include any of the following keywords: {timestamp}, {prompt}, {negative_prompt}, {width}, {height}, {seed}, {image_similarity}, {prompt_strength}, {steps}"
description=f"The filename template for generated images. Can include any of the following keywords: {'{' + '}, {'.join(config.filename_template_allowed_vars) + '}'}",
)
do_autosave_before_images: bpy.props.BoolProperty(
name="Save 'Before' Images",
Expand Down
34 changes: 21 additions & 13 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
example_dimensions = [512, 640, 768, 896, 960, 1024, 1280, 1344, 1600, 1920, 2048]
file_formats = {"JPEG": "jpg", "BMP": "bmp", "IRIS": "rgb", "PNG": "png", "JPEG2000": "jp2", "TARGA": "tga", "TARGA_RAW": "tga", "CINEON": "cin", "DPX": "dpx", "OPEN_EXR_MULTILAYER": "exr", "OPEN_EXR": "exr", "HDR": "hdr", "TIFF": "tif", "WEBP": "webp"}

max_filename_length = 128 # Most system have a max of 255, but we'll leave room
max_filename_length = 230


def get_addon_preferences(context=None):
Expand All @@ -51,37 +51,45 @@ def get_addon_preferences(context=None):
def create_temp_file(prefix, suffix=".png"):
return tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix).name

def sanitize_filename(filename):
# remove any characters that aren't alphanumeric, underscore, dash, or period
filename = re.sub(r'[^\w\-_\.]', '_', filename)

def sanitize_filename(filename, extra_length=0):
# remove any characters that aren't alphanumeric, space, underscore, dash, period, comma or parentheses
filename = re.sub(r'[^\w \-_\.(),]', '_', filename)
# remove any double underscores, dashes, periods
filename = re.sub(r'([-_\.]){2,}', r'\1', filename)
# remove any leading underscores and limit to max filename length
filename = filename.lstrip('_')[:max_filename_length]
# remove any trailing underscores, dashes, and periods
filename = filename.rstrip('_-.')
# limit to max filename length
filename = filename[:(max_filename_length - extra_length)]
return filename

def get_image_filename(scene, prompt, negative_prompt, suffix = ""):

def sanitize_filename_template(template):
# remove any {vars} that aren't in the list of allowed vars
return re.sub(r'{(.*?)}', lambda match: match.group(0) if match.group(1) in config.filename_template_allowed_vars else '', template)


def get_image_filename(scene, prompt, negative_prompt, suffix=""):
props = scene.air_props
timestamp = int(time.time())
template = props.image_filename_template
if not template:
template = config.default_image_filename_template

full_filename = f"{template}{suffix}".format(
template = sanitize_filename_template(template)

full_filename = f"{template}".format(
timestamp=timestamp,
prompt=prompt,
negative_prompt=negative_prompt,
width=get_output_width(scene),
height=get_output_height(scene),
seed=props.seed,
image_similarity=props.image_similarity,
prompt_strength=props.cfg_scale,
image_similarity=round(props.image_similarity, 2),
prompt_strength=round(props.cfg_scale, 2),
steps=props.steps,
)

return sanitize_filename(full_filename)
sanitized_filename = sanitize_filename(full_filename, len(suffix))
return sanitized_filename + suffix


def get_image_format(to_lower = True):
Expand Down

0 comments on commit 287c9a4

Please sign in to comment.