diff --git a/README.MD b/README.MD index 8454687..7637075 100644 --- a/README.MD +++ b/README.MD @@ -17,6 +17,7 @@ Nodes are divided into 5 groups according to their functions: LayerStyle, LayerC ## Update **If the dependency package error after updating, please reinstall the relevant dependency packages. for details, please refer to [here](https://github.com/chflame163/ComfyUI_LayerStyle/issues/5). +* Commit [ImageScaleByAspectRatio](#ImageScaleByAspectRatio) node, it can be scaled image or mask according to frame ratio. * Fix the bug of color gradation in [LUT Apply](#LUT) node rendering, and this node now support for log color space. *Please load the dedicated log lut file for the log color space image. * Commit [CreateGradientMask](#CreateGradientMask) node. Commit [LayerImageTransform](#LayerImageTransform) and [LayerMaskTransform](#LayerMaskTransform) nodes. * Commit [MaskEdgeUltraDetail](#MaskEdgeUltraDetail) node, it process rough masks to ultra fine edges.Commit [Exposure](#Exposure) node. @@ -394,6 +395,25 @@ Outputs: * mask: If there is a mask input, the scaled mask will be output. * original_size: The original size data of the image is used for subsequent node recovery. +### ImageScaleByAspectRatio +Scale the image or mask by aspect ratio. the scaled size can be rounded to a multiple of 8 or 16, and can be scaled to the long side size. +![image](image/image_scale_by_aspect_ratio_example.png) + +Node options: +![image](image/image_scale_by_aspect_ratio_node.png) +* aspect_ratio: Here are several common frame ratios provided. alternatively, you can customize the scale using "custom". +* proportional_width: Proportional width. if the aspect ratio option is not "custom", this setting will be ignored. +* proportional_height: Proportional height. if the aspect ratio option is not "custom", this setting will be ignored. +* fit: Scale aspect ratio mode. when the width to height ratio of the original image does not match the scaled size, there are three modes to choose from, +The _letterbox_ mode retains the complete frame and fills in the blank spaces with black; +The _crop_ mode retains the complete short edge, and any excess of the long edge will be cut off; +The _fill_ mode does not maintain frame ratio and fills the screen with width and height. +* method: Scaling sampling methods, including lanczos, bicubic, hamming, bilinear, box, and nearest. +* round_to_multiple: Round multiples. for example, setting it to 8 will force the width and height to be multiples of 8. +* scale_by_longest_side: Allow scaling by long edge size. +* longest_side: When the scale_by_longest_side is set to True, this will be used this value to the long edge of the image. when the original_size have input, this setting will be ignored. + + ### ImageShift Shift the image. this node supports the output of displacement seam masks, making it convenient to create continuous textures. ![image](image/image_shift_example.png) diff --git a/README_CN.MD b/README_CN.MD index 011dff8..bcb47fa 100644 --- a/README_CN.MD +++ b/README_CN.MD @@ -13,6 +13,7 @@ ## 更新说明 **如果本插件更新后出现依赖包错误,请重新安装相关依赖包。详情见[这里](https://github.com/chflame163/ComfyUI_LayerStyle/issues/5)。 +* 添加 [ImageScaleByAspectRatio](#ImageScaleByAspectRatio) 节点, 可按画幅比例缩放图像。 * 改正 [LUT Apply](#LUT) 节点渲染出现色阶的bug, 并增加log色彩空间支持。*log色彩空间图片请加载专门的log lut。 * 添加[CreateGradientMask](#CreateGradientMask) 节点。添加 [LayerImageTransform](#LayerImageTransform) 和 [LayerMaskTransform](#LayerMaskTransform) 节点。 * 添加[MaskEdgeUltraDetail](#MaskEdgeUltraDetail) 节点,给粗糙的遮罩进行处理获得超精细的边缘。添加 [Exposure](#Exposure) 节点,调整图像曝光。 @@ -375,6 +376,28 @@ *仅限输入image和mask, 如果强制接入其他类型输入,将导致节点错误。 +输出: +* image: 如果有image输入,将输出缩放后的图像。 +* mask: 如果有mask输入,将输出缩放后的遮罩。 +* original_size: 图像的原始大小数据,用于后续节点进行恢复。 + + +### ImageScaleByAspectRatio +将图像或遮罩按宽高比缩放。可设置将缩放后的尺寸按8或者16的倍数取整,可按长边尺寸缩放。 +![image](image/image_scale_by_aspect_ratio_example.png) + +节点选项说明: +![image](image/image_scale_by_aspect_ratio_node.png) +* aspect_ratio: 宽高比。此处提供了几个常见画幅比例。也可选"custom"自定义比例。 +* proportional_width: 比例宽。如果aspect_ratio选项不是"custom",此处设置将被忽略。 +* proportional_height: 比例高。如果aspect_ratio选项不是"custom",此处设置将被忽略。 +* fit: 缩放画幅宽高比模式。有3种模式可以选择, letterbox模式保留完整的画幅,空白处用黑色补足;crop模式保留完整的短边,长边超出部分将被切除;fill模式不保持画幅比例,宽高各自填满画面。 +* method: 缩放的采样方法,包括lanczos、bicubic、hamming、bilinear、box和nearest。 +* round_to_multiple: 倍数取整。例如设置为8,宽和高将强制设置为8的倍数。 +* scale_by_longest_side: 允许按长边尺寸缩放。 +* longest_side: scale_by_longest_side被设置为True时,此项将作为是图像长边的长度。 + + 输出: * image: 如果有image输入,将输出缩放后的图像。 * mask: 如果有mask输入,将输出缩放后的遮罩。 diff --git a/image/image_scale_by_aspect_ratio_example.png b/image/image_scale_by_aspect_ratio_example.png new file mode 100644 index 0000000..9b0a137 Binary files /dev/null and b/image/image_scale_by_aspect_ratio_example.png differ diff --git a/image/image_scale_by_aspect_ratio_node.png b/image/image_scale_by_aspect_ratio_node.png new file mode 100644 index 0000000..8d9f77f Binary files /dev/null and b/image/image_scale_by_aspect_ratio_node.png differ diff --git a/py/image_scale_by_aspect_ratio.py b/py/image_scale_by_aspect_ratio.py new file mode 100644 index 0000000..8401c18 --- /dev/null +++ b/py/image_scale_by_aspect_ratio.py @@ -0,0 +1,157 @@ +import torch + +from .imagefunc import * + +NODE_NAME = 'ImageScaleByAspectRatio' + +class ImageScaleByAspectRatio: + + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(self): + ratio_list = ['custom', '1:1', '3:2', '4:3', '16:9', '2:3', '3:4', '9:16'] + fit_mode = ['letterbox', 'crop', 'fill'] + method_mode = ['lanczos', 'bicubic', 'hamming', 'bilinear', 'box', 'nearest'] + multiple_list = ['8', '16', 'None'] + + return { + "required": { + "aspect_ratio": (ratio_list,), + "proportional_width": ("INT", {"default": 2, "min": 1, "max": 999, "step": 1}), + "proportional_height": ("INT", {"default": 1, "min": 1, "max": 999, "step": 1}), + "fit": (fit_mode,), + "method": (method_mode,), + "round_to_multiple": (multiple_list,), + "scale_to_longest_side": ("BOOLEAN", {"default": False}), # 鏄惁鎸夐暱杈圭缉鏀� + "longest_side": ("INT", {"default": 1024, "min": 4, "max": 999999, "step": 1}), + }, + "optional": { + "image": ("IMAGE",), # + "mask": ("MASK",), # + } + } + + RETURN_TYPES = ("IMAGE", "MASK", "BOX",) + RETURN_NAMES = ("image", "mask", "original_size") + FUNCTION = 'image_scale_by_aspect_ratio' + CATEGORY = '馃樅dzNodes/LayerUtility' + OUTPUT_NODE = True + + def image_scale_by_aspect_ratio(self, aspect_ratio, proportional_width, proportional_height, + fit, method, round_to_multiple, scale_to_longest_side, longest_side, + image=None, mask = None, + ): + orig_images = [] + orig_masks = [] + orig_width = 0 + orig_height = 0 + target_width = 0 + target_height = 0 + ratio = 1.0 + ret_images = [] + ret_masks = [] + if image is not None: + for i in image: + i = torch.unsqueeze(i, 0) + orig_images.append(i) + orig_width, orig_height = tensor2pil(orig_images[0]).size + if mask is not None: + if mask.dim() == 2: + mask = torch.unsqueeze(mask, 0) + for m in mask: + m = torch.unsqueeze(m, 0) + orig_masks.append(m) + _width, _height = tensor2pil(orig_masks[0]).size + if (orig_width > 0 and orig_width != _width) or (orig_height > 0 and orig_height != _height): + log(f"Error: {NODE_NAME} skipped, because the mask is does'nt match image.", message_type='error') + return (None, None,) + elif orig_width + orig_height == 0: + orig_width = _width + orig_height = _height + + if orig_width + orig_height == 0: + log(f"Error: {NODE_NAME} skipped, because the image or mask at least one must be input.", message_type='error') + return (None, None,) + + if aspect_ratio == 'custom': + ratio = proportional_width / proportional_height + else: + s = aspect_ratio.split(":") + ratio = int(s[0]) / int(s[1]) + + # calculate target width and height + if orig_width > orig_height: + if scale_to_longest_side: + target_width = longest_side + else: + target_width = orig_width + target_height = int(target_width / ratio) + else: + if scale_to_longest_side: + target_height = longest_side + else: + target_height = orig_height + target_width = int(target_height * ratio) + + if ratio < 1: + if scale_to_longest_side: + _r = longest_side / target_height + target_height = longest_side + else: + _r = orig_height / target_height + target_height = orig_height + target_width = int(target_width * _r) + + + if round_to_multiple != 'None': + multiple = int(round_to_multiple) + target_width = num_round_to_multiple(target_width, multiple) + target_height = num_round_to_multiple(target_height, multiple) + + _mask = Image.new('L', size=(target_width, target_height), color='black') + _image = Image.new('RGB', size=(target_width, target_height), color='black') + + resize_sampler = Image.LANCZOS + if method == "bicubic": + resize_sampler = Image.BICUBIC + elif method == "hamming": + resize_sampler = Image.HAMMING + elif method == "bilinear": + resize_sampler = Image.BILINEAR + elif method == "box": + resize_sampler = Image.BOX + elif method == "nearest": + resize_sampler = Image.NEAREST + + if len(orig_images) > 0: + for i in orig_images: + _image = tensor2pil(i).convert('RGB') + _image = fit_resize_image(_image, target_width, target_height, fit, resize_sampler) + ret_images.append(pil2tensor(_image)) + if len(orig_masks) > 0: + for m in orig_masks: + _mask = tensor2pil(m).convert('L') + _mask = fit_resize_image(_mask, target_width, target_height, fit, resize_sampler).convert('L') + ret_masks.append(image2mask(_mask)) + if len(ret_images) > 0 and len(ret_masks) >0: + log(f"{NODE_NAME} Processed {len(ret_images)} image(s).", message_type='finish') + return (torch.cat(ret_images, dim=0), torch.cat(ret_masks, dim=0),) + elif len(ret_images) > 0 and len(ret_masks) == 0: + log(f"{NODE_NAME} Processed {len(ret_images)} image(s).", message_type='finish') + return (torch.cat(ret_images, dim=0), None,) + elif len(ret_images) == 0 and len(ret_masks) > 0: + log(f"{NODE_NAME} Processed {len(ret_masks)} image(s).", message_type='finish') + return (None, torch.cat(ret_masks, dim=0),) + else: + log(f"Error: {NODE_NAME} skipped, because the available image or mask is not found.", message_type='error') + return (None, None,) + +NODE_CLASS_MAPPINGS = { + "LayerUtility: ImageScaleByAspectRatio": ImageScaleByAspectRatio +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "LayerUtility: ImageScaleByAspectRatio": "LayerUtility: ImageScaleByAspectRatio" +} \ No newline at end of file diff --git a/py/imagefunc.py b/py/imagefunc.py index 2338e1b..2ea9b80 100644 --- a/py/imagefunc.py +++ b/py/imagefunc.py @@ -925,6 +925,16 @@ def random_numbers(total:int, random_range:int, seed:int=0, sum_of_numbers:int=0 ret_list.append((sum_of_numbers - sum(ret_list)) // 2) return ret_list +def num_round_to_multiple(number:int, multiple:int) -> int: + remainder = number % multiple + if remainder == 0 : + return number + else: + factor = int(number / multiple) + if number - factor * multiple > multiple / 2: + factor += 1 + return factor * multiple + '''CLASS''' class AnyType(str):