对齐Image Scale Restore参数与实现,修正image_combine_alpha通道解包bug

This commit is contained in:
Cyber Dick Lang
2025-07-01 20:17:37 +08:00
parent ed0c58cd5d
commit 33e49af04f
2 changed files with 50 additions and 20 deletions
+1 -1
View File
@@ -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))
+49 -19
View File
@@ -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))