From f741ef0252a19a4532fa9fa6365e248628ce0809 Mon Sep 17 00:00:00 2001 From: Kijai <40791699+kijai@users.noreply.github.com> Date: Tue, 14 May 2024 13:07:42 +0300 Subject: [PATCH] Add ImageResizeKJ --- __init__.py | 1 + nodes/image_nodes.py | 59 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index bba36b7..679873c 100644 --- a/__init__.py +++ b/__init__.py @@ -58,6 +58,7 @@ NODE_CONFIG = { "ImagePass": {"class": ImagePass}, "ImagePadForOutpaintMasked": {"class": ImagePadForOutpaintMasked, "name": "Image Pad For Outpaint Masked"}, "ImagePadForOutpaintTargetSize": {"class": ImagePadForOutpaintTargetSize, "name": "Image Pad For Outpaint Target Size"}, + "ImageResizeKJ": {"class": ImageResizeKJ, "name": "Resize Image"}, "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 ddc51d5..41080af 100644 --- a/nodes/image_nodes.py +++ b/nodes/image_nodes.py @@ -1262,4 +1262,61 @@ class PreviewAnimation: counter += 1 animated = num_frames != 1 - return { "ui": { "images": results, "animated": (animated,), "text": [f"{num_frames}x{pil_images[0].size[0]}x{pil_images[0].size[1]}"] } } \ No newline at end of file + return { "ui": { "images": results, "animated": (animated,), "text": [f"{num_frames}x{pil_images[0].size[0]}x{pil_images[0].size[1]}"] } } + +class ImageResizeKJ: + upscale_methods = ["nearest-exact", "bilinear", "area", "bicubic", "lanczos"] + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "width": ("INT", { "default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 8, }), + "height": ("INT", { "default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 8, }), + "upscale_method": (s.upscale_methods,), + "keep_proportion": ("BOOLEAN", { "default": False }), + "divisible_by": ("INT", { "default": 2, "min": 0, "max": 512, "step": 1, }), + }, + "optional" : { + "width_input": ("INT", { "forceInput": True}), + "height_input": ("INT", { "forceInput": True}), + "get_image_size": ("IMAGE",), + } + } + + RETURN_TYPES = ("IMAGE", "INT", "INT",) + RETURN_NAMES = ("IMAGE", "width", "height",) + FUNCTION = "resize" + CATEGORY = "KJNodes/image" + DESCRIPTION = """ +Resizes the image to the specified width and height. +""" + + def resize(self, image, width, height, keep_proportion, upscale_method, divisible_by, width_input=None, height_input=None, get_image_size=None): + B, H, W, C = image.shape + if width_input: + width = width_input + if height_input: + height = height_input + if get_image_size is not None: + _, width, height, _ = get_image_size.shape + + if keep_proportion: + ratio = min(width / W, height / H) + width = round(W * ratio) + height = round(H * ratio) + else: + if width == 0: + width = W + if height == 0: + height = H + + if divisible_by > 1: + width = width - (width % divisible_by) + height = height - (height % divisible_by) + + image = image.movedim(-1,1) + scaled = common_upscale(image, width, height, upscale_method, 'disabled') + scaled = scaled.movedim(1,-1) + + return(scaled, scaled.shape[2], scaled.shape[1],) \ No newline at end of file