From 16b50fb11897864fde8eb5f90e90a2d60e88bd3a Mon Sep 17 00:00:00 2001 From: keit Date: Fri, 30 May 2025 13:46:27 +0700 Subject: [PATCH] =?UTF-8?q?=E3=82=A2=E3=83=AB=E3=83=95=E3=82=A1=E3=83=81?= =?UTF-8?q?=E3=83=A3=E3=83=B3=E3=83=8D=E3=83=AB=E3=82=92=E3=82=B0=E3=83=AC?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=82=B1=E3=83=BC=E3=83=AB=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=8F=9B=E3=81=99=E3=82=8B=E3=83=8E=E3=83=BC=E3=83=89=E3=81=A8?= =?UTF-8?q?=E3=80=81=E3=82=A2=E3=83=AB=E3=83=95=E3=82=A1=E3=83=81=E3=83=A3?= =?UTF-8?q?=E3=83=B3=E3=83=8D=E3=83=AB=E3=82=92RGB=E3=81=AB=E3=83=95?= =?UTF-8?q?=E3=83=A9=E3=83=83=E3=83=88=E5=8C=96=E3=81=99=E3=82=8B=E3=83=8E?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 6 ++++ nodes/alpha_flatten.py | 59 ++++++++++++++++++++++++++++++++ nodes/alpha_to_grayscale.py | 68 +++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 nodes/alpha_flatten.py create mode 100644 nodes/alpha_to_grayscale.py diff --git a/__init__.py b/__init__.py index 6f7f8c3..cce848f 100644 --- a/__init__.py +++ b/__init__.py @@ -4,6 +4,8 @@ from .nodes.binarize_image import BinarizeImage from .nodes.binarize_image_using_otsu import BinarizeImageUsingOtsu from .nodes.antialiasing_image import AntialiasingImage from .nodes.remove_white_background_noise import RemoveWhiteBackgroundNoise +from .nodes.alpha_to_grayscale import AlphaToGrayscale +from .nodes.alpha_flatten import AlphaFlatten NODE_CLASS_MAPPINGS = { "BrightnessTransparency": BrightnessTransparency, @@ -12,6 +14,8 @@ NODE_CLASS_MAPPINGS = { "GrayscaleImage": GrayscaleImage, "AntialiasingImage": AntialiasingImage, "RemoveWhiteBackgroundNoise": RemoveWhiteBackgroundNoise, + "AlphaToGrayscale": AlphaToGrayscale, + "AlphaFlatten": AlphaFlatten, } NODE_DISPLAY_NAME_MAPPINGS = { @@ -21,6 +25,8 @@ NODE_DISPLAY_NAME_MAPPINGS = { "GrayscaleImage": "GrayscaleImage", "AntialiasingImage": "AntialiasingImage", "RemoveWhiteBackgroundNoise": "RemoveWhiteBackgroundNoise", + "AlphaToGrayscale": "AlphaToGrayscale", + "AlphaFlatten": "AlphaFlatten", } __all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"] diff --git a/nodes/alpha_flatten.py b/nodes/alpha_flatten.py new file mode 100644 index 0000000..9262f84 --- /dev/null +++ b/nodes/alpha_flatten.py @@ -0,0 +1,59 @@ +import numpy as np +import torch + + +class AlphaFlatten: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "images": ( + "IMAGE", + { + "tooltip": "Input images to flatten alpha channel into RGB colors." + }, + ), + } + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "run" + CATEGORY = "ComfyUI-Image-Toolkit" + + def run(self, images): + is_tensor = isinstance(images, torch.Tensor) + + if is_tensor: + if images.device.type != "cpu": + images = images.cpu() + images_np = images.numpy() + else: + images_np = images + + result_images = [] + + for image in images_np: + # アルファチャンネルがある場合のみ処理 + if image.shape[2] == 4: + # RGB値を取得 + rgb = image[:, :, :3] + # アルファ値を取得 + alpha = image[:, :, 3] + + # 白背景とのalpha合成処理 + # result = foreground_color * alpha + background_color * (1 - alpha) + # 白背景:background_color = 1.0 + flattened_rgb = rgb * alpha[:, :, np.newaxis] + 1.0 * (1.0 - alpha[:, :, np.newaxis]) + + result_images.append(flattened_rgb) + else: + # アルファチャンネルがない場合はそのまま返す + result_images.append(image) + + result_np = np.stack(result_images) + result_tensor = torch.from_numpy(result_np) + + if is_tensor and images.device.type != "cpu": + result_tensor = result_tensor.to(images.device) + + return (result_tensor,) diff --git a/nodes/alpha_to_grayscale.py b/nodes/alpha_to_grayscale.py new file mode 100644 index 0000000..2bc451d --- /dev/null +++ b/nodes/alpha_to_grayscale.py @@ -0,0 +1,68 @@ +import numpy as np +import torch + + +class AlphaToGrayscale: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "images": ( + "IMAGE", + { + "tooltip": "Input images to convert alpha to grayscale." + }, + ), + } + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "run" + CATEGORY = "ComfyUI-Image-Toolkit" + + def run(self, images): + is_tensor = isinstance(images, torch.Tensor) + + if is_tensor: + if images.device.type != "cpu": + images = images.cpu() + images_np = images.numpy() + else: + images_np = images + + result_images = [] + + for image in images_np: + # アルファチャンネルがある場合のみ処理 + if image.shape[2] == 4: + # アルファ値を取得 + alpha = image[:, :, 3] + + # アルファ値をグレースケール値に変換(BrightnessTransparencyの逆変換) + # BrightnessTransparency: alpha = 1.0 - grayscale + # AlphaToGrayscale: grayscale = 1.0 - alpha + grayscale = 1.0 - alpha + + # RGB画像を作成(透明度なし) + rgb_image = np.zeros( + (image.shape[0], image.shape[1], 3), dtype=np.float32 + ) + + # 全チャンネルにグレースケール値を設定 + rgb_image[:, :, 0] = grayscale + rgb_image[:, :, 1] = grayscale + rgb_image[:, :, 2] = grayscale + + result_images.append(rgb_image) + else: + # アルファチャンネルがない場合はそのまま返す + result_images.append(image) + + result_np = np.stack(result_images) + result_tensor = torch.from_numpy(result_np) + + if is_tensor and images.device.type != "cpu": + result_tensor = result_tensor.to(images.device) + + return (result_tensor,) +