From d7c945fa90de7fc715ef05e70cf46f3199785ae4 Mon Sep 17 00:00:00 2001 From: keit Date: Thu, 27 Feb 2025 23:04:51 +0900 Subject: [PATCH] =?UTF-8?q?=E7=94=BB=E5=83=8F=E3=81=AE2=E5=80=A4=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodes.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/nodes.py b/nodes.py index d8ca0e5..6ffba38 100644 --- a/nodes.py +++ b/nodes.py @@ -58,10 +58,67 @@ class BrightnessTransparency: return (result_tensor,) +class BinarizeImage: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "images": ("IMAGE", {"tooltip": "Images to binarize."}), + "threshold": ( + "INT", + { + "default": 127, + "min": 0, + "max": 255, + "step": 1, + "tooltip": "Values above threshold become white (1.0), below become black (0.0).", + }, + ), + } + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "run" + CATEGORY = "ComfyUI-Image-Toolkit" + + def run(self, images, threshold): + 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 = [] + normalized_threshold = threshold / 255.0 + + for image in images_np: + processed_image = image.copy() + grayscale = np.mean(image[:, :, :3], axis=2) + binary_mask = np.where(grayscale >= normalized_threshold, 1.0, 0.0) + + for i in range(3): + processed_image[:, :, i] = binary_mask + + result_images.append(processed_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, + "BinarizeImage": BinarizeImage, } NODE_DISPLAY_NAME_MAPPINGS = { "BrightnessTransparency": "BrightnessTransparency", + "BinarizeImage": "BinarizeImage", }