From b8cbcd9fd30169fbd8cd5b6f031c0d49856f7082 Mon Sep 17 00:00:00 2001 From: keit Date: Thu, 27 Feb 2025 22:58:09 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B0=E3=83=AC=E3=83=BC=E3=82=B9=E3=82=B1?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E5=8C=96=E3=81=97=E3=81=A6=E9=80=8F=E9=81=8E?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=99=E3=82=8B=E3=83=8E=E3=83=BC=E3=83=89?= =?UTF-8?q?=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 | 3 +++ nodes.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 __init__.py create mode 100644 nodes.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..39a8c6b --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS + +__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"] diff --git a/nodes.py b/nodes.py new file mode 100644 index 0000000..d8ca0e5 --- /dev/null +++ b/nodes.py @@ -0,0 +1,67 @@ +import numpy as np +import torch + + +class BrightnessTransparency: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "images": ( + "IMAGE", + { + "tooltip": "Input images to adjust transparency. Bright areas will become transparent." + }, + ), + } + } + + 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] == 3: + rgba_image = np.zeros( + (image.shape[0], image.shape[1], 4), dtype=np.float32 + ) + rgba_image[:, :, :3] = image + rgba_image[:, :, 3] = 1.0 # Range 0.0-1.0 + else: + rgba_image = image.copy() + + # Set alpha values inversely proportional to brightness (bright=transparent, dark=opaque) + brightness = np.mean(rgba_image[:, :, :3], axis=2) + alpha = 1.0 - brightness + rgba_image[:, :, 3] = alpha + + result_images.append(rgba_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,) + + +NODE_CLASS_MAPPINGS = { + "BrightnessTransparency": BrightnessTransparency, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "BrightnessTransparency": "BrightnessTransparency", +}