Integration with frame-correction

exit_image
Charles Fettinger 2023-04-30 22:52:36 -07:00
parent 3a1cfd53d5
commit c975fe8896
9 changed files with 610 additions and 240 deletions

View File

@ -7,7 +7,7 @@ import modules.sd_models
import gradio as gr
from scripts import postprocessing_upscale
from .static_variables import jsonprompt_schemafile
import asyncio
def fix_env_Path_ffprobe():
envpath = os.environ["PATH"]
@ -90,6 +90,9 @@ def do_upscaleImg(curImg, upscale_do, upscaler_name, upscale_by):
)
return pp.image
async def showGradioErrorAsync(txt, delay=1):
await asyncio.sleep(delay) # sleep for 1 second
raise gr.Error(txt)
def validatePromptJson_throws(data):
with open(jsonprompt_schemafile, "r") as s:
@ -104,8 +107,10 @@ def putPrompts(files):
data = json.loads(file_contents)
validatePromptJson_throws(data)
return [
gr.Textbox.update(data["commonPromptPrefix"]),
gr.DataFrame.update(data["prompts"]),
gr.Textbox.update(data["negPrompt"]),
gr.Textbox.update(data["commonPromptSuffix"]),
gr.Textbox.update(data["negPrompt"])
]
except Exception:
@ -115,13 +120,15 @@ def putPrompts(files):
print(
"[InfiniteZoom:] Loading your prompt failed. It seems to be invalid. Your prompt table is preserved."
)
return [gr.DataFrame.update(), gr.Textbox.update()]
return [gr.Textbox.update(), gr.DataFrame.update(), gr.Textbox.update(),gr.Textbox.update()]
def clearPrompts():
return [
gr.DataFrame.update(value=[[0, "Infinite Zoom. Start over"]]),
gr.Textbox.update(""),
gr.Textbox.update(""),
gr.Textbox.update("")
]
def value_to_bool(value):

View File

@ -1,4 +1,4 @@
from PIL import Image, ImageDraw, ImageEnhance
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageDraw, ImageFont
import requests
import base64
import numpy as np
@ -30,6 +30,15 @@ def shrink_and_paste_on_blank(current_image, mask_width, mask_height):
def open_image(image_path):
"""
Opens an image from a file path or URL, or decodes a DataURL string into an image.
Parameters:
image_path (str): The file path, URL, or DataURL string of the image to open.
Returns:
Image: A PIL Image object of the opened image.
"""
if image_path.startswith('http'):
# If the image path is a URL, download the image using requests
response = requests.get(image_path)
@ -46,6 +55,16 @@ def open_image(image_path):
return img
def apply_alpha_mask(image, mask_image):
"""
Applies a mask image as the alpha channel of the input image.
Parameters:
image (Image): A PIL Image object of the image to apply the mask to.
mask_image (Image): A PIL Image object of the alpha mask to apply.
Returns:
Image: A PIL Image object of the input image with the applied alpha mask.
"""
# Resize the mask to match the current image size
mask_image = mask_image.resize(image.size)
# Apply the mask as the alpha layer of the current image
@ -53,7 +72,25 @@ def apply_alpha_mask(image, mask_image):
result_image.putalpha(mask_image.convert('L')) # convert to grayscale
return result_image
def resize_image_with_aspect_ratio(image, basewidth=512, baseheight=512):
def resize_image_with_aspect_ratio(image: Image, basewidth: int = 512, baseheight: int = 512) -> Image:
"""
Resizes an image while maintaining its aspect ratio. This may not fill the entire image height.
Args:
- image (PIL.Image): The input image.
- basewidth (int): The desired width of the output image. Defaults to 512.
- baseheight (int): The desired height of the output image. Defaults to 512.
Returns:
- PIL.Image: The resized image.
Raises:
- ValueError: If `basewidth` or `baseheight` is less than or equal to 0.
"""
if basewidth <= 0 or baseheight <= 0:
raise ValueError("resize_image_with_aspect_ratio error: basewidth and baseheight must be greater than 0")
# Get the original size of the image
orig_width, orig_height = image.size
@ -81,29 +118,46 @@ def resize_image_with_aspect_ratio(image, basewidth=512, baseheight=512):
return resized_image
def resize_and_crop_image(image, new_width=512, new_height=512):
def resize_and_crop_image(image: Image, new_width: int = 512, new_height: int = 512) -> Image:
"""
Resizes and crops an image to a specified width and height. This ensures that the entire new_width and new_height
dimensions are filled by the image, and the aspect ratio is maintained.
Parameters:
- image (PIL.Image): The image to be resized and cropped.
- new_width (int): The desired width of the new image. Default is 512.
- new_height (int): The desired height of the new image. Default is 512.
Returns:
- cropped_image (PIL.Image): The resized and cropped image.
"""
# Get the dimensions of the original image
orig_width, orig_height = image.size
orig_width, orig_height = image.size
# Calculate the aspect ratios of the original and new images
orig_aspect_ratio = orig_width / float(orig_height)
new_aspect_ratio = new_width / float(new_height)
new_aspect_ratio = new_width / float(new_height)
# Calculate the new size of the image while maintaining aspect ratio
if orig_aspect_ratio > new_aspect_ratio:
# The original image is wider than the new image, so we need to crop the sides
resized_width = int(new_height * orig_aspect_ratio)
resized_height = new_height
left_offset = (resized_width - new_width) / 2
left_offset = (resized_width - new_width) // 2
top_offset = 0
else:
# The original image is taller than the new image, so we need to crop the top and bottom
resized_width = new_width
resized_height = int(new_width / orig_aspect_ratio)
left_offset = 0
top_offset = (resized_height - new_height) / 2
top_offset = (resized_height - new_height) // 2
# Resize the image with Lanczos resampling filter
resized_image = image.resize((resized_width, resized_height), resample=Image.LANCZOS)
resized_image = image.resize((resized_width, resized_height), resample=Image.LANCZOS)
# Crop the image to fill the entire height and width of the new image
cropped_image = resized_image.crop((left_offset, top_offset, left_offset + new_width, top_offset + new_height))
cropped_image = resized_image.crop((left_offset, top_offset, left_offset + new_width, top_offset + new_height))
return cropped_image
def grayscale_to_gradient(image, gradient_colors):
@ -286,4 +340,103 @@ def draw_gradient_ellipse(width=512, height=512, white_amount=1.0, rotation = 0.
#image.paste(inner_ellipse, center, mask=inner_ellipse)
# Creating object of Brightness class
# Return the result image
return image
return image
def crop_fethear_ellipse(image: Image.Image, feather_margin: int = 30, width_offset: int = 0, height_offset: int = 0) -> Image.Image:
"""
Crop an elliptical region from the input image with a feathered edge.
Args:
image (PIL.Image.Image): The input image.
feather_margin (int): The size of the feathered edge, in pixels. Default is 30.
width_offset (int): The offset from the left and right edges of the image to the elliptical region. Default is 0.
height_offset (int): The offset from the top and bottom edges of the image to the elliptical region. Default is 0.
Returns:
A new PIL Image containing the cropped elliptical region with a feathered edge.
"""
# Create a blank mask image with the same size as the original image
mask = Image.new("L", image.size, 0)
draw = ImageDraw.Draw(mask)
# Calculate the ellipse's bounding box
ellipse_box = (
width_offset,
height_offset,
image.width - width_offset,
image.height - height_offset,
)
# Draw the ellipse on the mask
draw.ellipse(ellipse_box, fill=255)
# Apply the mask to the original image
result = Image.new("RGBA", image.size)
result.paste(image, mask=mask)
# Crop the resulting image to the ellipse's bounding box
cropped_image = result.crop(ellipse_box)
# Create a new mask image with a black background (0)
mask = Image.new("L", cropped_image.size, 0)
draw = ImageDraw.Draw(mask)
# Draw an ellipse on the mask image with a feathered edge
draw.ellipse(
(
0 + feather_margin,
0 + feather_margin,
cropped_image.width - feather_margin,
cropped_image.height - feather_margin,
),
fill=255,
outline=0,
)
# Apply a Gaussian blur to the mask image
mask = mask.filter(ImageFilter.GaussianBlur(radius=feather_margin / 2))
cropped_image.putalpha(mask)
# Paste the cropped image onto a new image with the same size as the input image
res = Image.new(cropped_image.mode, (image.width, image.height))
paste_pos = (
int((res.width - cropped_image.width) / 2),
int((res.height - cropped_image.height) / 2),
)
res.paste(cropped_image, paste_pos)
return res
def crop_inner_image(image: Image, width_offset: int, height_offset: int) -> Image:
"""
Crops an input image to the center, with the specified width and height offsets.
Args:
image (PIL.Image): The input image to be cropped.
width_offset (int): The width offset used for cropping.
height_offset (int): The height offset used for cropping.
Returns:
PIL.Image: The cropped image, resized to the original image size using Lanczos resampling.
"""
# Get the size of the input image
width, height = image.size
# Calculate the center coordinates of the image
center_x, center_y = int(width / 2), int(height / 2)
# Crop the image to the center using the specified offsets
cropped_image = image.crop(
(
center_x - width_offset,
center_y - height_offset,
center_x + width_offset,
center_y + height_offset,
)
)
# Resize the cropped image to the original image size using Lanczos resampling
resized_image = cropped_image.resize((width, height), resample=Image.LANCZOS)
return resized_image

View File

@ -1,58 +1,77 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"prompts": {
"type": "object",
"properties": {
"data": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "1.2",
"type": "object",
"properties": {
"prompts": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items": [
{
"oneOf": [
{
"type": "integer",
"minimum": 0
},
{
"type": "string"
}
]
},
{
"type": "string"
},
{
"type": "string"
},
{
"type": "string"
},
{
"type": "boolean"
}
],
"minItems": 0,
"maxItems": 999,
"uniqueItems": false
},
"minItems": 0
"items": [
{
"oneOf": [
{
"type": "integer",
"minimum": 0
},
{
"type": "string"
}
]
},
{
"type": "string"
},
{
"type": "string"
},
{
"type": "string"
},
{
"oneOf": [
{
"type": "boolean"
},
{
"type": "string"
}
]
}
],
"minItems": 0,
"maxItems": 999,
"uniqueItems": false
},
"headers": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 5
}
"minItems": 0
},
"required": ["data", "headers"]
"headers": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 2
}
},
"negPrompt": {
"type": "string"
}
"required": [ "data", "headers" ]
},
"required": ["prompts", "negPrompt"]
}
"negPrompt": {
"type": "string"
},
"commonPromptPrefix": {
"type": "string"
},
"commonPromptSuffix": {
"type": "string"
}
},
"required": [
"prompts",
"negPrompt",
"commonPromptPrefix",
"commonPromptSuffix"
]
}

View File

@ -3,6 +3,7 @@ import numpy as np
from PIL import Image
from modules.ui import plaintext_to_html
import modules.shared as shared
from modules.processing import Processed, StableDiffusionProcessing
from .helpers import (
fix_env_Path_ffprobe,
@ -11,12 +12,148 @@ from .helpers import (
do_upscaleImg,value_to_bool
)
from .sd_helpers import renderImg2Img, renderTxt2Img
from .image import shrink_and_paste_on_blank, open_image, apply_alpha_mask, draw_gradient_ellipse, resize_and_crop_image
from .image import shrink_and_paste_on_blank, open_image, apply_alpha_mask, draw_gradient_ellipse, resize_and_crop_image, crop_fethear_ellipse, crop_inner_image
from .video import write_video
def outpaint_steps(
width,
height,
common_prompt_pre,
common_prompt_suf,
prompts,
prompt_images,
prompt_alpha_mask_images,
prompt_image_is_keyframe,
negative_prompt,
seed,
sampler,
num_inference_steps,
guidance_scale,
inpainting_denoising_strength,
inpainting_mask_blur,
inpainting_fill_mode,
inpainting_full_res,
inpainting_padding,
init_img,
outpaint_steps,
out_config,
mask_width,
mask_height,
custom_exit_image,
frame_correction=True, # TODO: add frame_Correction in UI
):
main_frames = [init_img.convert("RGB")]
for i in range(outpaint_steps):
print_out = (
"Outpaint step: "
+ str(i + 1)
+ " / "
+ str(outpaint_steps)
+ " Seed: "
+ str(seed)
)
print(print_out)
current_image = main_frames[-1]
# apply available alpha mask of previous image
if prompt_alpha_mask_images[max(k for k in prompt_alpha_mask_images.keys() if k <= (i + 1))] != "":
current_image = apply_alpha_mask(current_image, open_image(prompt_alpha_mask_images[max(k for k in prompt_alpha_mask_images.keys() if k <= (i + 1))]))
else:
#generate automatic alpha mask
current_image_gradient_ratio = 0.615 #max((min(current_image.width/current_image.height,current_image.height/current_image.width) * 0.89),0.1)
current_image = apply_alpha_mask(current_image, draw_gradient_ellipse(current_image.width, current_image.height, current_image_gradient_ratio, 0.0, 3.0).convert("RGB"))
prev_image = shrink_and_paste_on_blank(
current_image, mask_width, mask_height
)
current_image = prev_image
mask_image = np.array(current_image)[:, :, 3]
mask_image = Image.fromarray(255 - mask_image).convert("RGB")
# create mask (black image with white mask_width width edges)
# inpainting step
current_image = current_image.convert("RGB")
#keyframes are not inpainted
paste_previous_image = not prompt_image_is_keyframe[max(k for k in prompt_image_is_keyframe.keys() if k <= (i + 1))]
if custom_exit_image and ((i + 1) == outpaint_steps):
current_image = resize_and_crop_image(custom_exit_image, width, height)
main_frames.append(current_image.convert("RGB"))
print("using Custom Exit Image")
save2Collect(current_image, out_config, f"exit_img.png")
else:
if prompt_images[max(k for k in prompt_images.keys() if k <= (i + 1))] == "":
pr = prompts[max(k for k in prompts.keys() if k <= i)]
processed, seed = renderImg2Img(
f"{common_prompt_pre}\n{pr}\n{common_prompt_suf}".strip(),
negative_prompt,
sampler,
num_inference_steps,
guidance_scale,
seed,
width,
height,
current_image,
mask_image,
inpainting_denoising_strength,
inpainting_mask_blur,
inpainting_fill_mode,
inpainting_full_res,
inpainting_padding,
)
if len(processed.images) > 0:
main_frames.append(processed.images[0].convert("RGB"))
save2Collect(processed.images[0], out_config, f"outpain_step_{i}.png")
paste_previous_image = True
else:
# use prerendered image, known as keyframe. Resize to target size
print(f"image {i} is a keyframe")
current_image = open_image(prompt_images[max(k for k in prompt_images.keys() if k <= (i + 1))])
main_frames.append(resize_and_crop_image(current_image, width, height).convert("RGB"))
save2Collect(current_image, out_config, f"key_frame_{i}.png")
#seed = newseed
# TODO: seed behavior
if frame_correction and inpainting_mask_blur > 0:
corrected_frame = crop_inner_image(
main_frames[i + 1], mask_width, mask_height
)
enhanced_img = crop_fethear_ellipse(
main_frames[i],
30,
inpainting_mask_blur / 3 // 2,
inpainting_mask_blur / 3 // 2,
)
save2Collect(main_frames[i], out_config, f"main_frame_{i}")
save2Collect(enhanced_img, out_config, f"main_frame_enhanced_{i}")
corrected_frame.paste(enhanced_img, mask=enhanced_img)
main_frames[i] = corrected_frame
else: #TEST
# apply available alpha mask of previous image
#if prompt_alpha_mask_images[max(k for k in prompt_alpha_mask_images.keys() if k <= (i + 1))] != "":
# current_image = apply_alpha_mask(current_image, open_image(prompt_alpha_mask_images[max(k for k in prompt_alpha_mask_images.keys() if k <= (i + 1))]))
#else:
# current_image_gradient_ratio = 0.65 #max((min(current_image.width/current_image.height,current_image.height/current_image.width) * 0.925),0.1)
# current_image = draw_gradient_ellipse(current_image.width, current_image.height, current_image_gradient_ratio, 0.0, 1.8).convert("RGB")
# paste previous image on current image
if paste_previous_image:
current_image.paste(prev_image, mask=prev_image)
return main_frames
def create_zoom(
common_prompt_pre,
prompts_array,
common_prompt_suf,
negative_prompt,
num_outpainting_steps,
guidance_scale,
@ -46,7 +183,9 @@ def create_zoom(
for i in range(batchcount):
print(f"Batch {i+1}/{batchcount}")
result = create_zoom_single(
common_prompt_pre,
prompts_array,
common_prompt_suf,
negative_prompt,
num_outpainting_steps,
guidance_scale,
@ -75,8 +214,49 @@ 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)}")
def frames2Collect(all_frames, out_config):
for i, f in enumerate(all_frames):
save2Collect(f, out_config, f"frame_{i}")
def create_zoom_single(
common_prompt_pre,
prompts_array,
common_prompt_suf,
negative_prompt,
num_outpainting_steps,
guidance_scale,
@ -109,6 +289,7 @@ def create_zoom_single(
# except Exception:
# pass
fix_env_Path_ffprobe()
out_config = prepare_output_path()
prompts = {}
prompt_images = {}
@ -145,14 +326,16 @@ def create_zoom_single(
if custom_init_image:
current_image = resize_and_crop_image(custom_init_image, width, height)
print("using Custom Initial Image")
save2Collect(current_image, out_config, f"init_custom.png")
processed = Processed(StableDiffusionProcessing(),images_list=[current_image], seed=current_seed, info="init_custom image")
else:
if prompt_images[min(k for k in prompt_images.keys() if k >= 0)] == "":
load_model_from_setting(
"infzoom_txt2img_model", progress, "Loading Model for txt2img: "
)
pr = prompts[min(k for k in prompts.keys() if k >= 0)]
processed, current_seed = renderTxt2Img(
prompts[min(k for k in prompts.keys() if k >= 0)],
f"{common_prompt_pre}\n{pr}\n{common_prompt_suf}".strip(),
negative_prompt,
sampler,
num_inference_steps,
@ -161,10 +344,15 @@ def create_zoom_single(
width,
height,
)
current_image = processed.images[0]
if len(processed.images) > 0:
current_image = processed.images[0]
save2Collect(current_image, out_config, f"init_txt2img.png")
else:
print("using image 0 as Initial keyframe")
current_image = open_image(prompt_images[min(k for k in prompt_images.keys() if k >= 0)])
current_image = resize_and_crop_image(current_image, width, height)
save2Collect(current_image, out_config, f"init_custom.png")
processed = Processed(StableDiffusionProcessing(),images_list=[current_image], seed=current_seed, info="prompt image")
mask_width = math.trunc(width / 4) # was initially 512px => 128px
mask_height = math.trunc(height / 4) # was initially 512px => 128px
@ -176,99 +364,55 @@ def create_zoom_single(
if upscale_do and progress:
progress(0, desc="upscaling inital image")
all_frames.append(
do_upscaleImg(current_image, upscale_do, upscaler_name, upscale_by)
if upscale_do
else current_image
load_model_from_setting(
"infzoom_inpainting_model", progress, "Loading Model for inpainting/img2img: "
)
load_model_from_setting("infzoom_inpainting_model", progress, "Loading Model for inpainting/img2img: " )
if custom_exit_image:
extra_frames += 2
for i in range(num_outpainting_steps + extra_frames):
print_out = (
"Outpaint step: "
+ str(i + 1)
+ " / "
+ str(num_outpainting_steps + extra_frames)
+ " Seed: "
+ str(current_seed)
)
print(print_out)
if progress:
progress(((i + 1) / num_outpainting_steps), desc=print_out)
main_frames = outpaint_steps(
width,
height,
common_prompt_pre,
common_prompt_suf,
prompts,
prompt_images,
prompt_alpha_mask_images,
prompt_image_is_keyframe,
negative_prompt,
current_seed,
sampler,
num_inference_steps,
guidance_scale,
inpainting_denoising_strength,
inpainting_mask_blur,
inpainting_fill_mode,
inpainting_full_res,
inpainting_padding,
current_image,
num_outpainting_steps + extra_frames,
out_config,
mask_width,
mask_height,
custom_exit_image,
)
# apply available alpha mask of previous image
if prompt_alpha_mask_images[max(k for k in prompt_alpha_mask_images.keys() if k <= (i + 1))] != "":
current_image = apply_alpha_mask(current_image, open_image(prompt_alpha_mask_images[max(k for k in prompt_alpha_mask_images.keys() if k <= (i + 1))]))
else:
#generate automatic alpha mask
current_image_gradient_ratio = 0.615 #max((min(current_image.width/current_image.height,current_image.height/current_image.width) * 0.89),0.1)
current_image = apply_alpha_mask(current_image, draw_gradient_ellipse(current_image.width, current_image.height, current_image_gradient_ratio, 0.0, 3.0).convert("RGB"))
prev_image_fix = current_image
prev_image = shrink_and_paste_on_blank(current_image, mask_width, mask_height)
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")
# inpainting step
current_image = current_image.convert("RGB")
paste_previous_image = not prompt_image_is_keyframe[max(k for k in prompt_image_is_keyframe.keys() if k <= (i + 1))]
# Custom and specified images work like keyframes
if custom_exit_image and (i + 1) >= (num_outpainting_steps + extra_frames):
current_image = resize_and_crop_image(custom_exit_image, width, height)
print("using Custom Exit Image")
else:
if prompt_images[max(k for k in prompt_images.keys() if k <= (i + 1))] == "":
processed, current_seed = renderImg2Img(
prompts[max(k for k in prompts.keys() if k <= (i + 1))],
negative_prompt,
sampler,
num_inference_steps,
guidance_scale,
current_seed,
width,
height,
current_image,
mask_image,
inpainting_denoising_strength,
inpainting_mask_blur,
inpainting_fill_mode,
inpainting_full_res,
inpainting_padding,
)
current_image = processed.images[0]
# only paste previous image when generating a new image
#current_image.paste(prev_image, mask=prev_image)
paste_previous_image = True
else:
# use prerendered image, known as keyframe. Resize to target size
current_image = open_image(prompt_images[max(k for k in prompt_images.keys() if k <= (i + 1))])
current_image = resize_and_crop_image(current_image, width, height)
# apply available alpha mask of previous image
#if prompt_alpha_mask_images[max(k for k in prompt_alpha_mask_images.keys() if k <= (i + 1))] != "":
# current_image = apply_alpha_mask(current_image, open_image(prompt_alpha_mask_images[max(k for k in prompt_alpha_mask_images.keys() if k <= (i + 1))]))
#else:
# current_image_gradient_ratio = 0.65 #max((min(current_image.width/current_image.height,current_image.height/current_image.width) * 0.925),0.1)
# current_image = draw_gradient_ellipse(current_image.width, current_image.height, current_image_gradient_ratio, 0.0, 1.8).convert("RGB")
# paste previous image on current image
if paste_previous_image:
current_image.paste(prev_image, mask=prev_image)
all_frames.append(
do_upscaleImg(main_frames[0], upscale_do, upscaler_name, upscale_by)
if upscale_do
else main_frames[0]
)
for i in range(len(main_frames) - 1):
print(f"processing frame {i}")
# interpolation steps between 2 inpainted images (=sequential zoom and crop)
for j in range(num_interpol_frames - 1):
current_image = main_frames[i + 1]
interpol_image = current_image
save2Collect(interpol_image, out_config, f"interpol_img_{i}_{j}].png")
interpol_width = round(
interpol_width = math.ceil(
(
1
- (1 - 2 * mask_width / width)
@ -278,7 +422,7 @@ def create_zoom_single(
/ 2
)
interpol_height = round(
interpol_height = math.ceil(
(
1
- (1 - 2 * mask_height / height)
@ -298,25 +442,27 @@ def create_zoom_single(
)
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(
interpol_width2 = math.ceil(
(1 - (width - 2 * mask_width) / (width - 2 * interpol_width))
/ 2
* width
)
interpol_height2 = round(
interpol_height2 = math.ceil(
(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, interpol_height2
main_frames[i], interpol_width2, interpol_height2
)
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")
@ -336,19 +482,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")
)
print("save to: " + save_path)
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,
@ -357,8 +494,8 @@ def create_zoom_single(
)
return (
out,
processed.images,
out_config["video_filename"],
main_frames,
processed.js(),
plaintext_to_html(processed.info),
plaintext_to_html(""),

View File

@ -2,6 +2,7 @@ from modules.processing import (
process_images,
StableDiffusionProcessingTxt2Img,
StableDiffusionProcessingImg2Img,
Processed
)
import modules.shared as shared
@ -72,5 +73,10 @@ def renderImg2Img(
# p.latent_mask = Image.new("RGB", (p.width, p.height), "white")
processed = process_images(p)
# For those that use Image grids this will make sure that ffmpeg does not crash out
if (len(processed.images) > 1) and (processed.images[0].size[0] != processed.images[-1].size[0]):
processed.images.pop(0)
print("\nGrid image detected applying patch")
newseed = p.seed
return processed, newseed
return processed, newseed

View File

@ -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,14 @@ 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,
),
)

View File

@ -4,13 +4,15 @@ import modules.sd_samplers
default_prompt = """
{
"commonPromptPrefix":"<lora:epiNoiseoffset_v2:0.6> ",
"prompts":{
"headers":["outpaint steps","prompt","image location","blend mask location", "is keyframe"],
"data":[
[0,"Huge spectacular Waterfall in a dense tropical forest,epic perspective,(vegetation overgrowth:1.3)(intricate, ornamentation:1.1),(baroque:1.1), fantasy, (realistic:1) digital painting , (magical,mystical:1.2) , (wide angle shot:1.4), (landscape composed:1.2)(medieval:1.1), divine,cinematic,(tropical forest:1.4),(river:1.3)mythology,india, volumetric lighting, Hindu ,epic, Alex Horley Wenjun Lin greg rutkowski Ruan Jia (Wayne Barlowe:1.2) <lora:epiNoiseoffset_v2:0.6> ","C:\\path\\to\\image.png", "C:\\path\\to\\mask_image.png", false]
]
},
"negPrompt":"frames, borderline, text, character, duplicate, error, out of frame, watermark, low quality, ugly, deformed, blur bad-artist"
"commonPromptSuffix":"style by Alex Horley Wenjun Lin greg rutkowski Ruan Jia (Wayne Barlowe:1.2)",
"negPrompt":"frames, border, edges, text, character, duplicate, error, out of frame, watermark, low quality, ugly, deformed, blur bad-artist"
}
"""
available_samplers = [
@ -18,7 +20,7 @@ available_samplers = [
]
empty_prompt = (
'{"prompts":{"data":[],"headers":["outpaint steps","prompt","image location", "blend mask location", "is keyframe"]},"negPrompt":""}'
'{"prompts":{"data":[],"headers":["outpaint steps","prompt","image location", "blend mask location", "is keyframe"]},"negPrompt":"", commonPromptPrefix:"", commonPromptSuffix:""}'
)
invalid_prompt = {
@ -27,7 +29,14 @@ invalid_prompt = {
"headers": ["outpaint steps", "prompt","image location","blend mask location", "is keyframe"],
},
"negPrompt": "Invalid prompt-json",
"commonPromptPrefix": "Invalid prompt",
"commonPromptSuffix": "Invalid prompt"
}
available_samplers = [
s.name for s in modules.sd_samplers.samplers if "UniPc" not in s.name
]
current_script_dir = scripts.basedir().split(os.sep)[
-2:
] # contains install and our extension foldername

View File

@ -30,14 +30,23 @@ def on_ui_tabs():
with gr.Row():
with gr.Column(scale=1, variant="panel"):
with gr.Tab("Main"):
main_outpaint_steps = gr.Slider(
minimum=2,
maximum=100,
step=1,
value=8,
label="Total Outpaint Steps",
info="The more it is, the longer your videos will be",
)
with gr.Row():
batchcount_slider = gr.Slider(
minimum=1,
maximum=25,
value=shared.opts.data.get("infzoom_batchcount", 1),
step=1,
label="Batch Count",
)
main_outpaint_steps = gr.Slider(
minimum=2,
maximum=100,
step=1,
value=8,
label="Total Outpaint Steps",
info="The more it is, the longer your videos will be",
)
# safe reading json prompt
pr = shared.opts.data.get("infzoom_defPrompt", default_prompt)
@ -49,6 +58,9 @@ def on_ui_tabs():
except Exception:
jpr = invalid_prompt
main_common_prompt_pre = gr.Textbox(
value=jpr["commonPromptPrefix"], label="Common Prompt Prefix"
)
main_prompts = gr.Dataframe(
type="array",
headers=["outpaint step", "prompt", "image location", "blend mask", "is keyframe"],
@ -59,6 +71,10 @@ def on_ui_tabs():
wrap=True,
)
main_common_prompt_suf = gr.Textbox(
value=jpr["commonPromptSuffix"], label="Common Prompt Suffix"
)
main_negative_prompt = gr.Textbox(
value=jpr["negPrompt"], label="Negative Prompt"
)
@ -79,12 +95,22 @@ def on_ui_tabs():
exportPrompts_button.click(
None,
_js="exportPrompts",
inputs=[main_prompts, main_negative_prompt],
inputs=[
main_common_prompt_pre,
main_prompts,
main_common_prompt_suf,
main_negative_prompt,
],
outputs=None,
)
importPrompts_button.upload(
fn=putPrompts,
outputs=[main_prompts, main_negative_prompt],
outputs=[
main_common_prompt_pre,
main_prompts,
main_common_prompt_suf,
main_negative_prompt,
],
inputs=[importPrompts_button],
)
@ -97,59 +123,59 @@ def on_ui_tabs():
clearPrompts_button.click(
fn=clearPrompts,
inputs=[],
outputs=[main_prompts, main_negative_prompt],
outputs=[
main_prompts,
main_negative_prompt,
main_common_prompt_pre,
main_common_prompt_suf,
],
)
with gr.Row():
seed = gr.Number(
label="Seed", value=-1, precision=0, interactive=True
)
main_sampler = gr.Dropdown(
label="Sampler",
choices=available_samplers,
value="Euler a",
type="value",
)
with gr.Row():
main_width = gr.Slider(
minimum=16,
maximum=2048,
value=shared.opts.data.get("infzoom_outsizeW", 512),
step=16,
label="Output Width",
)
main_height = gr.Slider(
minimum=16,
maximum=2048,
value=shared.opts.data.get("infzoom_outsizeH", 512),
step=16,
label="Output Height",
)
with gr.Row():
main_guidance_scale = gr.Slider(
minimum=0.1,
maximum=15,
step=0.1,
value=7,
label="Guidance Scale",
)
sampling_step = gr.Slider(
minimum=1,
maximum=100,
step=1,
value=50,
label="Sampling Steps for each outpaint",
)
with gr.Row():
init_image = gr.Image(type="pil", label="custom initial image")
exit_image = gr.Image(type="pil", label="custom exit image")
batchcount_slider = gr.Slider(
minimum=1,
maximum=25,
value=shared.opts.data.get("infzoom_batchcount", 1),
step=1,
label="Batch Count",
)
with gr.Accordion("Render settings"):
with gr.Row():
seed = gr.Number(
label="Seed", value=-1, precision=0, interactive=True
)
main_sampler = gr.Dropdown(
label="Sampler",
choices=available_samplers,
value="Euler a",
type="value",
)
with gr.Row():
main_width = gr.Slider(
minimum=16,
maximum=2048,
value=shared.opts.data.get("infzoom_outsizeW", 512),
step=16,
label="Output Width",
)
main_height = gr.Slider(
minimum=16,
maximum=2048,
value=shared.opts.data.get("infzoom_outsizeH", 512),
step=16,
label="Output Height",
)
with gr.Row():
main_guidance_scale = gr.Slider(
minimum=0.1,
maximum=15,
step=0.1,
value=7,
label="Guidance Scale",
)
sampling_step = gr.Slider(
minimum=1,
maximum=100,
step=1,
value=50,
label="Sampling Steps for each outpaint",
)
with gr.Row():
init_image = gr.Image(type="pil", label="custom initial image")
exit_image = gr.Image(type="pil", label="custom exit image")
with gr.Tab("Video"):
video_frame_rate = gr.Slider(
label="Frames per second",
@ -237,7 +263,9 @@ Our best experience and trade-off is the R-ERSGAn4x upscaler.
generate_btn.click(
fn=wrap_gradio_gpu_call(create_zoom, extra_outputs=[None, "", ""]),
inputs=[
main_common_prompt_pre,
main_prompts,
main_common_prompt_suf,
main_negative_prompt,
main_outpaint_steps,
main_guidance_scale,

View File

@ -1,9 +1,9 @@
// Function to download data to a file
function exportPrompts(p, np, filename = "infinite-zoom-prompts.json") {
function exportPrompts(cppre, p, cpsuf, np, filename = "infinite-zoom-prompts.json") {
let J = { prompts: p, negPrompt: np }
let J = { prompts: p, negPrompt: np, commonPromptPrefix: cppre, commonPromptSuffix: cpsuf }
var file = new Blob([JSON.stringify(J)], { type: "text/csv" });
var file = new Blob([JSON.stringify(J,null,2)], { type: "text/csv" });
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others