Skip to content

Commit d301836

Browse files
committed
can select prior output for init_img using -1, -2, etc
1 parent 70aa674 commit d301836

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

scripts/dream.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ def main():
113113

114114
def main_loop(t2i, outdir, prompt_as_dir, parser, infile):
115115
"""prompt/read/execute loop"""
116-
done = False
117-
last_seeds = []
118-
path_filter = re.compile(r'[<>:"/\\|?*]')
116+
done = False
117+
path_filter = re.compile(r'[<>:"/\\|?*]')
118+
last_results = list()
119119

120120
# os.pathconf is not available on Windows
121121
if hasattr(os, 'pathconf'):
@@ -183,13 +183,23 @@ def main_loop(t2i, outdir, prompt_as_dir, parser, infile):
183183
if len(opt.prompt) == 0:
184184
print('Try again with a prompt!')
185185
continue
186+
if opt.init_img is not None and re.match('^-\d+$',opt.init_img): # retrieve previous value!
187+
try:
188+
opt.init_img = last_results[int(opt.init_img)][0]
189+
print(f'>> Reusing previous image {opt.init_img}')
190+
except IndexError:
191+
print(f'>> No previous initial image at position {opt.init_img} found')
192+
opt.init_img = None
193+
continue
194+
186195
if opt.seed is not None and opt.seed < 0: # retrieve previous value!
187196
try:
188-
opt.seed = last_seeds[opt.seed]
189-
print(f'reusing previous seed {opt.seed}')
197+
opt.seed = last_results[opt.seed][1]
198+
print(f'>> Reusing previous seed {opt.seed}')
190199
except IndexError:
191-
print(f'No previous seed at position {opt.seed} found')
200+
print(f'>> No previous seed at position {opt.seed} found')
192201
opt.seed = None
202+
continue
193203

194204
do_grid = opt.grid or t2i.grid
195205

@@ -240,10 +250,10 @@ def main_loop(t2i, outdir, prompt_as_dir, parser, infile):
240250
current_outdir = outdir
241251

242252
# Here is where the images are actually generated!
253+
last_results = []
243254
try:
244255
file_writer = PngWriter(current_outdir)
245256
prefix = file_writer.unique_prefix()
246-
seeds = list()
247257
results = [] # list of filename, prompt pairs
248258
grid_images = dict() # seed -> Image, only used if `do_grid`
249259
def image_writer(image, seed, upscaled=False):
@@ -274,15 +284,14 @@ def image_writer(image, seed, upscaled=False):
274284
if (not upscaled) or opt.save_original:
275285
# only append to results if we didn't overwrite an earlier output
276286
results.append([path, metadata_prompt])
277-
278-
seeds.append(seed)
287+
last_results.append([path,seed])
279288

280289
t2i.prompt2image(image_callback=image_writer, **vars(opt))
281290

282291
if do_grid and len(grid_images) > 0:
283-
grid_img = make_grid(list(grid_images.values()))
284-
first_seed = next(iter(seeds))
285-
filename = f'{prefix}.{first_seed}.png'
292+
grid_img = make_grid(list(grid_images.values()))
293+
first_seed = last_results[0][1]
294+
filename = f'{prefix}.{first_seed}.png'
286295
# TODO better metadata for grid images
287296
normalized_prompt = PromptFormatter(t2i, opt).normalize_prompt()
288297
metadata_prompt = f'{normalized_prompt} -S{first_seed} --grid -N{len(grid_images)}'
@@ -291,8 +300,6 @@ def image_writer(image, seed, upscaled=False):
291300
)
292301
results = [[path, metadata_prompt]]
293302

294-
last_seeds = list(seeds)
295-
296303
except AssertionError as e:
297304
print(e)
298305
continue

0 commit comments

Comments
 (0)