diff --git a/src/comfyui_ino_nodes/utils/extra_nodes.py b/src/comfyui_ino_nodes/utils/extra_nodes.py index b5058f1..d2ddf2b 100644 --- a/src/comfyui_ino_nodes/utils/extra_nodes.py +++ b/src/comfyui_ino_nodes/utils/extra_nodes.py @@ -1,5 +1,8 @@ import random import json +import torch +from torchvision.transforms import InterpolationMode +import torchvision.transforms.functional as TorchFunctional import hashlib from datetime import datetime, timezone from ..node_helper import any_typ @@ -381,6 +384,121 @@ class InoSaveImages: return (names, len(results), ) +class InoImageResizeByLongerSideV1: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "size": ("INT", {"default": 512, "min": 0, "step": 1, "max": 99999}), + "interpolation_mode": ( + ["bicubic", "bilinear", "nearest", "nearest exact"], + ), + } + } + + RETURN_TYPES = ("IMAGE",) + RETURN_NAMES = ("Result",) + + FUNCTION = "function" + CATEGORY = "InoNodes" + + def function( + self, + image: torch.Tensor, + size: int, + interpolation_mode: str, + ): + assert isinstance(image, torch.Tensor) + assert isinstance(size, int) + assert isinstance(interpolation_mode, str) + + interpolation_mode = interpolation_mode.upper().replace(" ", "_") + interpolation_mode = getattr(InterpolationMode, interpolation_mode) + + _, h, w, _ = image.shape + + if h >= w: + new_h = size + new_w = round(w * new_h / h) + else: # h < w + new_w = size + new_h = round(h * new_w / w) + + image = image.permute(0, 3, 1, 2) + image = TorchFunctional.resize( + image, + (new_h, new_w), + interpolation=interpolation_mode, + antialias=True, + ) + image = image.permute(0, 2, 3, 1) + + return (image,) + +import nodes +MAX_RESOLUTION = nodes.MAX_RESOLUTION + +class InoImageResizeByLongerSideAndCropV2: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "target_width": ("INT", { + "default": 512, + "min": 1, + "max": MAX_RESOLUTION, + "step": 1 + }), + "target_height": ("INT", { + "default": 512, + "min": 1, + "max": MAX_RESOLUTION, + "step": 1 + }), + "padding_color": (["white", "black"],), + "interpolation": (["area", "bicubic", "nearest-exact", "bilinear", "lanczos"],), + "crop": ("BOOLEAN", {"default": True}), + "position": (["top-left", "top-center", "center", "bottom-center", "bottom-right"],), + "x": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}), + "y": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}), + } + } + + RETURN_TYPES = ("IMAGE",) + RETURN_NAMES = ("Result",) + + FUNCTION = "function" + CATEGORY = "InoNodes" + + def function(self, image, target_width: int, target_height: int, padding_color, interpolation, crop, position, x, y): + from comfy_extras.nodes_images import GetImageSize, ImageCrop, ResizeAndPadImage + get_image_size = GetImageSize() + image_size = get_image_size.get_size(image) + source_width = int(image_size[0]) + source_height = int(image_size[1]) + + source_is_width_larger:bool = source_width > source_height + target_is_width_larger:bool = target_width > target_height + + if source_is_width_larger == True and target_is_width_larger == True: + resize_width:int = target_width + resize_height:int = round((target_width / source_width) * source_height) + else: + resize_height:int = target_height + resize_width:int = round((target_height / source_height) * source_width) + + resizer = ResizeAndPadImage() + resized_image = resizer.resize_and_pad(image, resize_width, resize_height, padding_color, interpolation) + + if not crop: + return (resized_image[0],) + + cropper = ImageCrop() + cropped_image = cropper.crop(resized_image[0], target_width, target_height, x, y) + + return (cropped_image[0],) LOCAL_NODE_CLASS = { @@ -395,6 +513,8 @@ LOCAL_NODE_CLASS = { "InoIntToFloat": InoIntToFloat, "InoFloatToInt": InoFloatToInt, "InoSaveImages": InoSaveImages, + "InoImageResizeByLongerSideV1": InoImageResizeByLongerSideV1, + "InoImageResizeByLongerSideAndCropV2": InoImageResizeByLongerSideAndCropV2, } LOCAL_NODE_NAME = { "InoNotBoolean": "Ino Not Boolean", @@ -408,4 +528,6 @@ LOCAL_NODE_NAME = { "InoIntToFloat": "Ino Int To Float", "InoFloatToInt": "Ino Float To Int", "InoSaveImages": "Ino Save Images", + "InoImageResizeByLongerSideV1": "Ino Image Resize By Longer Side V1", + "InoImageResizeByLongerSideAndCropV2": "Ino Image Resize By Longer Side And Crop V2", }