diff --git a/iz_helpers/run.py b/iz_helpers/run.py index ed15bfd..fcb05d3 100644 --- a/iz_helpers/run.py +++ b/iz_helpers/run.py @@ -14,7 +14,6 @@ from .sd_helpers import renderImg2Img, renderTxt2Img from .image import shrink_and_paste_on_blank from .video import write_video - def create_zoom( common_prompt_pre, prompts_array, @@ -79,6 +78,40 @@ def create_zoom( return result + +def prepare_output_path(): + + isCollect = shared.opts.data.get("infzoom_collectAllResources",False) + output_path = shared.opts.data.get( + "infzoom_outpath", "output" + ) + + save_path = os.path.join( + output_path, shared.opts.data.get("infzoom_outSUBpath", "infinite-zooms") + ) + + if isCollect: + save_path = os.path.join(save_path,"iz_collect" + str(int(time.time()))) + + if not os.path.exists(save_path): + os.makedirs(save_path) + + video_filename = os.path.join(save_path,"infinite_zoom_" + str(int(time.time())) + ".mp4") + + return {"isCollect":isCollect,"save_path":save_path,"video_filename":video_filename} + + +def save2Collect(img, out_config, name): + if out_config["isCollect"]: + img.save(f'{out_config["save_path"]}/{name}.png') + +def frame2Collect(all_frames, out_config): + save2Collect(all_frames[-1], out_config, f"frame_{len(all_frames)}.png") + +def frames2Collect(all_frames, out_config): + for i,f in enumerate(all_frames): + save2Collect(f, out_config, f"frame_{i}.png") + def create_zoom_single( common_prompt_pre, prompts_array, @@ -116,7 +149,10 @@ def create_zoom_single( # pass fix_env_Path_ffprobe() + out_config = prepare_output_path() + prompts = {} + for x in prompts_array: try: key = int(x[0]) @@ -124,6 +160,7 @@ def create_zoom_single( prompts[key] = value except ValueError: pass + assert len(prompts_array) > 0, "prompts is empty" width = closest_upper_divisible_by_eight(outputsizeW) @@ -139,7 +176,9 @@ def create_zoom_single( current_image = custom_init_image.resize( (width, height), resample=Image.LANCZOS ) + save2Collect(current_image, out_config, f"init_img.png") print("using Custom Initial Image") + else: load_model_from_setting( "infzoom_txt2img_model", progress, "Loading Model for txt2img: " @@ -158,6 +197,8 @@ def create_zoom_single( ) if(len(processed.images) > 0): current_image = processed.images[0] + save2Collect(current_image, out_config, f"txt2img.png") + current_seed = newseed mask_width = math.trunc(width / 4) # was initially 512px => 128px @@ -194,12 +235,18 @@ def create_zoom_single( progress(((i + 1) / num_outpainting_steps), desc=print_out) prev_image_fix = current_image + save2Collect(prev_image_fix, out_config, f"prev_image_fix_{i}.png") + prev_image = shrink_and_paste_on_blank(current_image, mask_width, mask_height) + save2Collect(prev_image, out_config, f"prev_image_{1}.png") + current_image = prev_image # create mask (black image with white mask_width width edges) mask_image = np.array(current_image)[:, :, 3] mask_image = Image.fromarray(255 - mask_image).convert("RGB") + save2Collect(mask_image, out_config, f"mask_image_{i}.png") + # inpainting step current_image = current_image.convert("RGB") @@ -209,6 +256,7 @@ def create_zoom_single( (width, height), resample=Image.LANCZOS ) print("using Custom Exit Image") + save2Collect(current_image, out_config, f"exit_img.png") else: pr = prompts[max(k for k in prompts.keys() if k <= i)] processed, newseed = renderImg2Img( @@ -233,10 +281,12 @@ def create_zoom_single( current_seed = newseed if(len(processed.images) > 0): current_image.paste(prev_image, mask=prev_image) + save2Collect(current_image, out_config, f"curr_prev_paste_{i}.png") # interpolation steps between 2 inpainted images (=sequential zoom and crop) for j in range(num_interpol_frames - 1): interpol_image = current_image + save2Collect(interpol_image, out_config, f"interpol_img_{i}_{j}].png") interpol_width = round( ( @@ -266,8 +316,10 @@ def create_zoom_single( height - interpol_height, ) ) + save2Collect(interpol_image, out_config, f"interpol_crop_{i}_{j}.png") interpol_image = interpol_image.resize((width, height)) + save2Collect(interpol_image, out_config, f"interpol_resize_{i}_{j}.png") # paste the higher resolution previous image in the middle to avoid drop in quality caused by zooming interpol_width2 = round( @@ -285,8 +337,10 @@ def create_zoom_single( prev_image_fix_crop = shrink_and_paste_on_blank( prev_image_fix, interpol_width2, interpol_height2 ) + save2Collect(prev_image_fix, out_config, f"prev_image_fix_crop_{i}_{j}.png") interpol_image.paste(prev_image_fix_crop, mask=prev_image_fix_crop) + save2Collect(interpol_image, out_config, f"interpol_prevcrop_{i}_{j}.png") if upscale_do and progress: progress(((i + 1) / num_outpainting_steps), desc="upscaling interpol") @@ -306,18 +360,10 @@ def create_zoom_single( else current_image ) - video_file_name = "infinite_zoom_" + str(int(time.time())) + ".mp4" - output_path = shared.opts.data.get( - "infzoom_outpath", shared.opts.data.get("outdir_img2img_samples") - ) - save_path = os.path.join( - output_path, shared.opts.data.get("infzoom_outSUBpath", "infinite-zooms") - ) - if not os.path.exists(save_path): - os.makedirs(save_path) - out = os.path.join(save_path, video_file_name) + frames2Collect(all_frames, out_config) + write_video( - out, + out_config["video_filename"], all_frames, video_frame_rate, video_zoom_mode, @@ -326,7 +372,7 @@ def create_zoom_single( ) return ( - out, + out_config["video_filename"], processed.images, processed.js(), plaintext_to_html(processed.info), diff --git a/iz_helpers/settings.py b/iz_helpers/settings.py index 8c6af78..4338526 100644 --- a/iz_helpers/settings.py +++ b/iz_helpers/settings.py @@ -1,19 +1,19 @@ +import gradio as gr import modules.shared as shared from .static_variables import default_prompt -import gradio as gr def on_ui_settings(): section = ("infinite-zoom", "Infinite Zoom") shared.opts.add_option( - "outputs" "infzoom_outpath", + "infzoom_outpath", shared.OptionInfo( - "", + "outputs", "Path where to store your infinite video. Default is Outputs", gr.Textbox, {"interactive": True}, - section=section, + section=section, ), ) @@ -93,3 +93,18 @@ def on_ui_settings(): section=section, ), ) + + + shared.opts.add_option( + "infzoom_collectAllResources", + shared.OptionInfo( + False, + "Store all images (txt2img, init_image,exit_image, inpainting, interpolation) and the movie into one folder in your OUTPUT Path", + gr.Checkbox, + {"interactive": True}, + section=section, + ), + ) + + + diff --git a/scripts/infinite-zoom.py b/scripts/infinite-zoom.py index 1aaadab..50892d1 100644 --- a/scripts/infinite-zoom.py +++ b/scripts/infinite-zoom.py @@ -1,6 +1,7 @@ -from iz_helpers import on_ui_tabs, on_ui_settings from modules import script_callbacks +from iz_helpers import on_ui_tabs, on_ui_settings script_callbacks.on_ui_tabs(on_ui_tabs) script_callbacks.on_ui_settings(on_ui_settings) +