From 251e33e418d0d9dadae18d563beeb2034c41ad81 Mon Sep 17 00:00:00 2001 From: GeorgLegato Date: Sun, 16 Apr 2023 04:08:09 +0200 Subject: [PATCH] free ratio works, W and H mixed.. no clue why --- iz_helpers/image.py | 7 +++--- scripts/inifnite-zoom.py | 52 ++++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/iz_helpers/image.py b/iz_helpers/image.py index 8046829..8ac7b06 100644 --- a/iz_helpers/image.py +++ b/iz_helpers/image.py @@ -3,7 +3,7 @@ from PIL import Image import numpy as np -def shrink_and_paste_on_blank(current_image, mask_width): +def shrink_and_paste_on_blank(current_image, mask_width, mask_height): """ Decreases size of current_image by mask_width pixels from each side, then adds a mask_width width transparent frame, @@ -17,7 +17,7 @@ def shrink_and_paste_on_blank(current_image, mask_width): # shrink down by mask_width prev_image = current_image.resize( - (height-2*mask_width, width-2*mask_width)) + (width-2*mask_width, height-2*mask_height)) prev_image = prev_image.convert("RGBA") prev_image = np.array(prev_image) @@ -26,8 +26,7 @@ def shrink_and_paste_on_blank(current_image, mask_width): blank_image[:, :, 3] = 1 # paste shrinked onto blank - blank_image[mask_width:height-mask_width, - mask_width:width-mask_width, :] = prev_image + blank_image[mask_height:height-mask_height,mask_width:width-mask_width :] = prev_image prev_image = Image.fromarray(blank_image) return prev_image diff --git a/scripts/inifnite-zoom.py b/scripts/inifnite-zoom.py index e678ed1..72b4acb 100644 --- a/scripts/inifnite-zoom.py +++ b/scripts/inifnite-zoom.py @@ -106,7 +106,8 @@ def create_zoom( inpainting_full_res, inpainting_padding, zoom_speed, - outputsize + outputsizeW, + outputsizeH, ): prompts = {} for x in prompts_array: @@ -118,10 +119,10 @@ def create_zoom( pass assert len(prompts_array) > 0, "prompts is empty" - width = outputsize - height = outputsize + width = outputsizeW + height = outputsizeH - current_image = Image.new(mode="RGBA", size=(height, width)) + current_image = Image.new(mode="RGBA", size=(width, height)) mask_image = np.array(current_image)[:, :, 3] mask_image = Image.fromarray(255 - mask_image).convert("RGB") current_image = current_image.convert("RGB") @@ -143,6 +144,8 @@ def create_zoom( current_image = processed.images[0] mask_width = math.trunc(width/4) # was initially 512px => 128px + mask_height = math.trunc(height/4) # was initially 512px => 128px + num_interpol_frames = round(video_frame_rate * zoom_speed) all_frames = [] @@ -152,7 +155,7 @@ def create_zoom( prev_image_fix = current_image - prev_image = shrink_and_paste_on_blank(current_image, mask_width) + prev_image = shrink_and_paste_on_blank(current_image, mask_width, mask_height) current_image = prev_image @@ -185,34 +188,55 @@ def create_zoom( # interpolation steps bewteen 2 inpainted images (=sequential zoom and crop) for j in range(num_interpol_frames - 1): interpol_image = current_image + interpol_width = round( ( 1 - - (1 - 2 * mask_width / height) + - (1 - 2 * mask_width / width) + ** (1 - (j + 1) / num_interpol_frames) + ) + * width + / 2 + ) + + interpol_height = round( + ( + 1 + - (1 - 2 * mask_height / height) ** (1 - (j + 1) / num_interpol_frames) ) * height / 2 ) + interpol_image = interpol_image.crop( ( interpol_width, - interpol_width, + interpol_height, width - interpol_width, - height - interpol_width, + height - interpol_height, ) ) - interpol_image = interpol_image.resize((height, width)) + interpol_image = interpol_image.resize((width, height)) + # paste the higher resolution previous image in the middle to avoid drop in quality caused by zooming interpol_width2 = round( - (1 - (height - 2 * mask_width) / (height - 2 * interpol_width)) + (1 - (width - 2 * mask_width) / (width - 2 * interpol_width)) + / 2 + * width + ) + + interpol_height2 = round( + (1 - (height - 2 * mask_height) / (height - 2 * interpol_height)) / 2 * height ) + prev_image_fix_crop = shrink_and_paste_on_blank( - prev_image_fix, interpol_width2 + prev_image_fix, interpol_width2, interpol_height2 ) + interpol_image.paste(prev_image_fix_crop, mask=prev_image_fix_crop) all_frames.append(interpol_image) @@ -257,7 +281,8 @@ def on_ui_tabs(): with gr.Column(scale=1, variant="panel"): with gr.Tab("Main"): - outsize_slider = gr.Slider(minimum=512, maximum=2048,value=shared.opts.data.get("infzoom_outsize",512),step=8,label="Output size (square)") + outsizeW_slider = gr.Slider(minimum=64, maximum=2048,value=shared.opts.data.get("infzoom_outsizeW",512),step=16,label="Output Width") + outsizeH_slider = gr.Slider(minimum=64, maximum=2048,value=shared.opts.data.get("infzoom_outsizeH",512),step=16,label="Output Height") outpaint_prompts = gr.Dataframe( type="array", headers=["outpaint steps", "prompt"], @@ -380,7 +405,8 @@ def on_ui_tabs(): inpainting_full_res, inpainting_padding, zoom_speed_slider, - outsize_slider + outsizeH_slider, + outsizeW_slider ], outputs=[output_video, out_image, generation_info, html_info, html_log], )