Skip to content

Commit f0039cc

Browse files
authored
[Bugfix] truncate filenames in invokeai batch that exceed max filename length (#3143)
- This prevents `invokeai-batch` from trying to create image files whose names would exceed PC_NAME_MAX. - Closes #3115
2 parents d90aa42 + 8fa7d5c commit f0039cc

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

ldm/invoke/dynamic_prompts.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ def expand_prompts(
9999
sequence = 0
100100
for command in commands:
101101
sequence += 1
102+
format = _get_fn_format(outdir, sequence)
102103
parent_conn.send(
103-
command + f' --fnformat="dp.{sequence:04}.{{prompt}}.png"'
104+
command + f' --fnformat="{format}"'
104105
)
105106
parent_conn.close()
106107
else:
@@ -110,7 +111,17 @@ def expand_prompts(
110111
for p in children:
111112
p.terminate()
112113

113-
114+
def _get_fn_format(directory:str, sequence:int)->str:
115+
"""
116+
Get a filename that doesn't exceed filename length restrictions
117+
on the current platform.
118+
"""
119+
max_length = os.pathconf(directory,'PC_NAME_MAX')
120+
prefix = f'dp.{sequence:04}.'
121+
suffix = '.png'
122+
max_length -= len(prefix)+len(suffix)
123+
return f'{prefix}{{prompt:0.{max_length}}}{suffix}'
124+
114125
class MessageToStdin(object):
115126
def __init__(self, connection: Connection):
116127
self.connection = connection

0 commit comments

Comments
 (0)