From a153a07df1bf4f0a7d096210a97b00302cc03c9b Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Mon, 26 Feb 2024 21:24:11 +0200 Subject: [PATCH] Add RemapMaskRange --- nodes.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/nodes.py b/nodes.py index d54a060..1c8a6c6 100644 --- a/nodes.py +++ b/nodes.py @@ -3754,6 +3754,39 @@ class Intrinsic_lora_sampling: return (image_out, ) +class RemapMaskRange: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "mask": ("MASK",), + "min": ("FLOAT", {"default": 0.0,"min": -10.0, "max": 1.0, "step": 0.01}), + "max": ("FLOAT", {"default": 1.0,"min": 0.0, "max": 10.0, "step": 0.01}), + } + } + + RETURN_TYPES = ("MASK",) + RETURN_NAMES = ("mask",) + FUNCTION = "remap" + CATEGORY = "KJNodes/masking" + + def remap(self, mask, min, max): + + # Find the maximum value in the mask + mask_max = torch.max(mask) + + # If the maximum mask value is zero, avoid division by zero by setting it to 1 + mask_max = mask_max if mask_max > 0 else 1 + + # Scale the mask values to the new range defined by min and max + # The highest pixel value in the mask will be scaled to max + scaled_mask = (mask / mask_max) * (max - min) + min + + # Clamp the values to ensure they are within [0.0, 1.0] + scaled_mask = torch.clamp(scaled_mask, min=0.0, max=1.0) + + return (scaled_mask, ) + NODE_CLASS_MAPPINGS = { "INTConstant": INTConstant, "FloatConstant": FloatConstant, @@ -3823,6 +3856,7 @@ NODE_CLASS_MAPPINGS = { "EffnetEncode": EffnetEncode, "ImageNormalize_Neg1_To_1": ImageNormalize_Neg1_To_1, "Intrinsic_lora_sampling": Intrinsic_lora_sampling, + "RemapMaskRange": RemapMaskRange } NODE_DISPLAY_NAME_MAPPINGS = { "INTConstant": "INT Constant", @@ -3892,4 +3926,5 @@ NODE_DISPLAY_NAME_MAPPINGS = { "EffnetEncode": "EffnetEncode", "ImageNormalize_Neg1_To_1": "ImageNormalize_Neg1_To_1", "Intrinsic_lora_sampling": "Intrinsic_lora_sampling", + "RemapMaskRange": "RemapMaskRange", } \ No newline at end of file