From 33e49af04f901233e602555e44d90e2fc0c068e7 Mon Sep 17 00:00:00 2001 From: Cyber Dick Lang <286878701@qq.com> Date: Tue, 1 Jul 2025 20:17:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E9=BD=90Image=20Scale=20Restore?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=B8=8E=E5=AE=9E=E7=8E=B0=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3image=5Fcombine=5Falpha=E9=80=9A=E9=81=93=E8=A7=A3?= =?UTF-8?q?=E5=8C=85bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodes/image/image_combine_alpha.py | 2 +- nodes/image/image_scale_restore.py | 68 +++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/nodes/image/image_combine_alpha.py b/nodes/image/image_combine_alpha.py index 3da1c53..33cb5e2 100644 --- a/nodes/image/image_combine_alpha.py +++ b/nodes/image/image_combine_alpha.py @@ -76,7 +76,7 @@ class ImageCombineAlpha_UTK: for i in range(max_batch): _image = input_images[i] if i < len(input_images) else input_images[-1] _mask = input_masks[i] if i < len(input_masks) else input_masks[-1] - r, g, b, _ = image_channel_split(tensor2pil(_image).convert('RGB'), 'RGB') + r, g, b = image_channel_split(tensor2pil(_image).convert('RGB'), 'RGB') ret_image = image_channel_merge((r, g, b, tensor2pil(_mask).convert('L')), 'RGBA') ret_images.append(pil2tensor(ret_image)) diff --git a/nodes/image/image_scale_restore.py b/nodes/image/image_scale_restore.py index 8d53dff..507c74f 100644 --- a/nodes/image/image_scale_restore.py +++ b/nodes/image/image_scale_restore.py @@ -29,16 +29,19 @@ class ImageScaleRestore_UTK: @classmethod def INPUT_TYPES(cls): method_mode = ['lanczos', 'bicubic', 'hamming', 'bilinear', 'box', 'nearest'] + scale_to_list = ['None', 'longest', 'shortest', 'width', 'height', 'total_pixel(kilo pixel)'] + multiple_list = ['8', '16', '32', '64', '128', '256', '512', 'None'] return { "required": { - "image": ("IMAGE", ), # + "image": ("IMAGE", ), "scale": ("FLOAT", {"default": 1, "min": 0.01, "max": 100, "step": 0.01}), "method": (method_mode,), - "scale_by_longest_side": ("BOOLEAN", {"default": False}), # 是否按长边缩放 - "longest_side": ("INT", {"default": 1024, "min": 4, "max": 999999, "step": 1}), + "scale_to_side": (scale_to_list,), + "scale_to_length": ("INT", {"default": 1024, "min": 4, "max": 1e8, "step": 1}), + "round_to_multiple": (multiple_list,), }, "optional": { - "mask": ("MASK",), # + "mask": ("MASK",), "original_size": ("BOX",), } } @@ -48,10 +51,9 @@ class ImageScaleRestore_UTK: FUNCTION = 'image_scale_restore' def image_scale_restore(self, image, scale, method, - scale_by_longest_side, longest_side, - mask = None, original_size = None - ): - + scale_to_side, scale_to_length, round_to_multiple, + mask=None, original_size=None): + import math l_images = [] l_masks = [] ret_images = [] @@ -72,19 +74,50 @@ class ImageScaleRestore_UTK: max_batch = max(len(l_images), len(l_masks)) orig_width, orig_height = tensor2pil(l_images[0]).size + # 计算目标宽高 if original_size is not None: target_width = original_size[0] target_height = original_size[1] else: - target_width = int(orig_width * scale) - target_height = int(orig_height * scale) - if scale_by_longest_side: - if orig_width > orig_height: - target_width = longest_side - target_height = int(target_width * orig_height / orig_width) + # 参考 image scale by aspect 的逻辑 + ratio = orig_width / orig_height if orig_height != 0 else 1.0 + if scale_to_side == 'longest': + if orig_width >= orig_height: + target_width = scale_to_length + target_height = int(target_width / ratio) else: - target_height = longest_side - target_width = int(target_height * orig_width / orig_height) + target_height = scale_to_length + target_width = int(target_height * ratio) + elif scale_to_side == 'shortest': + if orig_width <= orig_height: + target_width = scale_to_length + target_height = int(target_width / ratio) + else: + target_height = scale_to_length + target_width = int(target_height * ratio) + elif scale_to_side == 'width': + target_width = scale_to_length + target_height = int(target_width / ratio) + elif scale_to_side == 'height': + target_height = scale_to_length + target_width = int(target_height * ratio) + elif scale_to_side == 'total_pixel(kilo pixel)': + target_width = math.sqrt(ratio * scale_to_length * 1000) + target_height = target_width / ratio + target_width = int(target_width) + target_height = int(target_height) + else: + target_width = int(orig_width * scale) + target_height = int(orig_height * scale) + + # 对齐到倍数 + if round_to_multiple != 'None': + multiple = int(round_to_multiple) + def num_round_up_to_multiple(num, multiple): + return ((num + multiple - 1) // multiple) * multiple + target_width = num_round_up_to_multiple(target_width, multiple) + target_height = num_round_up_to_multiple(target_height, multiple) + if target_width < 4: target_width = 4 if target_height < 4: @@ -102,16 +135,13 @@ class ImageScaleRestore_UTK: resize_sampler = Image.NEAREST for i in range(max_batch): - _image = l_images[i] if i < len(l_images) else l_images[-1] - _canvas = tensor2pil(_image).convert('RGB') ret_image = _canvas.resize((target_width, target_height), resize_sampler) ret_mask = Image.new('L', size=ret_image.size, color='white') if mask is not None: _mask = l_masks[i] if i < len(l_masks) else l_masks[-1] ret_mask = _mask.resize((target_width, target_height), resize_sampler) - ret_images.append(pil2tensor(ret_image)) ret_masks.append(image2mask(ret_mask))