Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 056bada

Browse files
authored
Merge pull request #526 from andreszs/main
New mask nodes for rectangular areas
2 parents fe7e088 + c8c7a31 commit 056bada

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@
183183
- Mask Floor Region: Return the lower most pixel values as white (255)
184184
- Mask Threshold Region: Apply a thresholded image between a black value and white value
185185
- Mask Gaussian Region: Apply a Gaussian blur to the mask
186+
- Mask Rect Area: Create a rectangular mask defined by percentages.
187+
- Mask Rect Area (Advanced): Create a rectangular mask defined by pixels and image size.
186188
- Masks Combine Masks: Combine 2 or more masks into one mask.
187189
- Masks Combine Batch: Combine batched masks into one mask.
188190
- Model Input Switch: Switch between two model inputs based on a boolean switch

WAS_Node_Suite.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8127,6 +8127,140 @@ def minority_region(self, masks, threshold=128):
81278127
return (region_tensor,)
81288128

81298129

8130+
# MASK RECT AREA
8131+
8132+
class WAS_Mask_Rect_Area:
8133+
# Creates a rectangle mask using percentage.
8134+
8135+
def __init__(self):
8136+
pass
8137+
8138+
@classmethod
8139+
def INPUT_TYPES(cls):
8140+
return {
8141+
"required": {
8142+
"x": ("INT", {"default": 0, "min": 0, "max": 100, "step": 1}),
8143+
"y": ("INT", {"default": 0, "min": 0, "max": 100, "step": 1}),
8144+
"width": ("INT", {"default": 50, "min": 0, "max": 100, "step": 1}),
8145+
"height": ("INT", {"default": 50, "min": 0, "max": 100, "step": 1}),
8146+
"blur_radius": ("INT", {"default": 0, "min": 0, "max": 255, "step": 1}),
8147+
},
8148+
"hidden": {"extra_pnginfo": "EXTRA_PNGINFO", "unique_id": "UNIQUE_ID"}
8149+
}
8150+
8151+
CATEGORY = "WAS Suite/Image/Masking"
8152+
8153+
RETURN_TYPES = ("MASK",)
8154+
RETURN_NAMES = ("MASKS",)
8155+
8156+
FUNCTION = "rect_mask"
8157+
8158+
def rect_mask(self, extra_pnginfo, unique_id, **kwargs):
8159+
# Get node values
8160+
min_x = kwargs["x"] / 100
8161+
min_y = kwargs["y"] / 100
8162+
width = kwargs["width"] / 100
8163+
height = kwargs["height"] / 100
8164+
blur_radius = kwargs["blur_radius"]
8165+
8166+
# Create a mask with standard resolution (e.g., 512x512)
8167+
resolution = 512
8168+
mask = torch.zeros((resolution, resolution))
8169+
8170+
# Calculate pixel coordinates
8171+
min_x_px = int(min_x * resolution)
8172+
min_y_px = int(min_y * resolution)
8173+
max_x_px = int((min_x + width) * resolution)
8174+
max_y_px = int((min_y + height) * resolution)
8175+
8176+
# Draw the rectangle on the mask
8177+
mask[min_y_px:max_y_px, min_x_px:max_x_px] = 1
8178+
8179+
# Apply blur if the radii are greater than 0
8180+
if blur_radius > 0:
8181+
dx = blur_radius * 2 + 1
8182+
dy = blur_radius * 2 + 1
8183+
8184+
# Convert the mask to a format compatible with OpenCV (numpy array)
8185+
mask_np = mask.cpu().numpy().astype("float32")
8186+
8187+
# Apply Gaussian Blur
8188+
blurred_mask = cv2.GaussianBlur(mask_np, (dx, dy), 0)
8189+
8190+
# Convert back to tensor
8191+
mask = torch.from_numpy(blurred_mask)
8192+
8193+
# Return the mask as a tensor with an additional channel
8194+
return (mask.unsqueeze(0),)
8195+
8196+
8197+
# MASK RECT AREA ADVANCED
8198+
8199+
class WAS_Mask_Rect_Area_Advanced:
8200+
# Creates a rectangle mask using pixels relative to image size.
8201+
8202+
def __init__(self):
8203+
pass
8204+
8205+
@classmethod
8206+
def INPUT_TYPES(cls):
8207+
return {
8208+
"required": {
8209+
"x": ("INT", {"default": 0, "min": 0, "max": 4096, "step": 64}),
8210+
"y": ("INT", {"default": 0, "min": 0, "max": 4096, "step": 64}),
8211+
"width": ("INT", {"default": 256, "min": 0, "max": 4096, "step": 64}),
8212+
"height": ("INT", {"default": 256, "min": 0, "max": 4096, "step": 64}),
8213+
"image_width": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 64}),
8214+
"image_height": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 64}),
8215+
"blur_radius": ("INT", {"default": 0, "min": 0, "max": 255, "step": 1}),
8216+
},
8217+
"hidden": {"extra_pnginfo": "EXTRA_PNGINFO", "unique_id": "UNIQUE_ID"}
8218+
}
8219+
8220+
CATEGORY = "WAS Suite/Image/Masking"
8221+
8222+
RETURN_TYPES = ("MASK",)
8223+
RETURN_NAMES = ("MASKS",)
8224+
8225+
FUNCTION = "rect_mask_adv"
8226+
8227+
def rect_mask_adv(self, extra_pnginfo, unique_id, **kwargs):
8228+
# Get node values
8229+
min_x = kwargs["x"]
8230+
min_y = kwargs["y"]
8231+
width = kwargs["width"]
8232+
height = kwargs["height"]
8233+
image_width = kwargs["image_width"]
8234+
image_height = kwargs["image_height"]
8235+
blur_radius = kwargs["blur_radius"]
8236+
8237+
# Calculate maximum coordinates
8238+
max_x = min_x + width
8239+
max_y = min_y + height
8240+
8241+
# Create a mask with the image dimensions
8242+
mask = torch.zeros((image_height, image_width))
8243+
8244+
# Draw the rectangle on the mask
8245+
mask[int(min_y):int(max_y), int(min_x):int(max_x)] = 1
8246+
8247+
# Apply blur if the radii are greater than 0
8248+
if blur_radius > 0:
8249+
dx = blur_radius * 2 + 1
8250+
dy = blur_radius * 2 + 1
8251+
8252+
# Convert the mask to a format compatible with OpenCV (numpy array)
8253+
mask_np = mask.cpu().numpy().astype("float32")
8254+
8255+
# Apply Gaussian Blur
8256+
blurred_mask = cv2.GaussianBlur(mask_np, (dx, dy), 0)
8257+
8258+
# Convert back to tensor
8259+
mask = torch.from_numpy(blurred_mask)
8260+
8261+
# Return the mask as a tensor with an additional channel
8262+
return (mask.unsqueeze(0),)
8263+
81308264

81318265
# MASK ARBITRARY REGION
81328266

@@ -14367,6 +14501,8 @@ def count_places(self, int_input):
1436714501
"Mask Gaussian Region": WAS_Mask_Gaussian_Region,
1436814502
"Mask Invert": WAS_Mask_Invert,
1436914503
"Mask Minority Region": WAS_Mask_Minority_Region,
14504+
"Mask Rect Area": WAS_Mask_Rect_Area,
14505+
"Mask Rect Area (Advanced)": WAS_Mask_Rect_Area_Advanced,
1437014506
"Mask Smooth Region": WAS_Mask_Smooth_Region,
1437114507
"Mask Threshold Region": WAS_Mask_Threshold_Region,
1437214508
"Masks Combine Regions": WAS_Mask_Combine,

0 commit comments

Comments
 (0)