From 218e95c8c9a7453a34ca610e67af08cf0ffb3db4 Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Sat, 11 May 2024 18:41:27 +0300 Subject: [PATCH] Add ImagePadForOutpaintTargetSize -node --- __init__.py | 1 + nodes/image_nodes.py | 60 ++++++++++++++++++++++++++++++++++++++++- web/js/spline_editor.js | 1 - 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/__init__.py b/__init__.py index 7954bdc..4b1f849 100644 --- a/__init__.py +++ b/__init__.py @@ -55,6 +55,7 @@ NODE_CONFIG = { "ImageNormalize_Neg1_To_1": {"class": ImageNormalize_Neg1_To_1, "name": "Image Normalize -1 to 1"}, "ImagePass": {"class": ImagePass}, "ImagePadForOutpaintMasked": {"class": ImagePadForOutpaintMasked, "name": "Image Pad For Outpaint Masked"}, + "ImagePadForOutpaintTargetSize": {"class": ImagePadForOutpaintTargetSize, "name": "Image Pad For Outpaint Target Size"}, "ImageUpscaleWithModelBatched": {"class": ImageUpscaleWithModelBatched, "name": "Image Upscale With Model Batched"}, "InsertImagesToBatchIndexed": {"class": InsertImagesToBatchIndexed, "name": "Insert Images To Batch Indexed"}, "MergeImageChannels": {"class": MergeImageChannels, "name": "Merge Image Channels"}, diff --git a/nodes/image_nodes.py b/nodes/image_nodes.py index 3a6611e..2a089e1 100644 --- a/nodes/image_nodes.py +++ b/nodes/image_nodes.py @@ -12,7 +12,7 @@ from PIL import ImageGrab, ImageDraw, ImageFont, Image from nodes import MAX_RESOLUTION, SaveImage from comfy_extras.nodes_mask import ImageCompositeMasked from comfy.cli_args import args -from comfy.utils import ProgressBar +from comfy.utils import ProgressBar, common_upscale import folder_paths import model_management @@ -752,6 +752,64 @@ class ImagePadForOutpaintMasked: return (new_image, new_mask,) else: return (new_image, mask,) + +class ImagePadForOutpaintTargetSize: + upscale_methods = ["nearest-exact", "bilinear", "area", "bicubic", "lanczos"] + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "target_width": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 8}), + "target_height": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 8}), + "feathering": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}), + "upscale_method": (s.upscale_methods,), + }, + "optional": { + "mask": ("MASK",), + } + } + + RETURN_TYPES = ("IMAGE", "MASK") + FUNCTION = "expand_image" + + CATEGORY = "image" + + def expand_image(self, image, target_width, target_height, feathering, upscale_method, mask=None): + B, H, W, C = image.size() + new_height = 0 + new_width = 0 + # Calculate the scaling factor while maintaining aspect ratio + scaling_factor = min(target_width / W, target_height / H) + + # Check if the image needs to be downscaled + if scaling_factor < 1: + image = image.movedim(-1,1) + # Calculate the new width and height after downscaling + new_width = int(W * scaling_factor) + new_height = int(H * scaling_factor) + + # Downscale the image + image_scaled = common_upscale(image, new_width, new_height, upscale_method, "disabled").movedim(1,-1) + if mask is not None: + mask_scaled = mask.unsqueeze(0) # Add an extra dimension for batch size + mask_scaled = F.interpolate(mask_scaled, size=(new_height, new_width), mode="nearest") + mask_scaled = mask_scaled.squeeze(0) # Remove the extra dimension after interpolation + else: + mask_scaled = mask + else: + # If downscaling is not needed, use the original image dimensions + image_scaled = image + mask_scaled = mask + + # Calculate how much padding is needed to reach the target dimensions + pad_top = max(0, (target_height - new_height) // 2) + pad_bottom = max(0, target_height - new_height - pad_top) + pad_left = max(0, (target_width - new_width) // 2) + pad_right = max(0, target_width - new_width - pad_left) + + # Now call the original expand_image with the calculated padding + return ImagePadForOutpaintMasked.expand_image(self, image_scaled, pad_left, pad_top, pad_right, pad_bottom, feathering, mask_scaled) class ImageAndMaskPreview(SaveImage): def __init__(self): diff --git a/web/js/spline_editor.js b/web/js/spline_editor.js index 3eb78ca..964f036 100644 --- a/web/js/spline_editor.js +++ b/web/js/spline_editor.js @@ -396,7 +396,6 @@ function createSplineEditor(context, reset=false) { context.setSize([w + 45, context.size[1]]); } vis.width(w); - updatePath(); } heightWidget.callback = () => {