Skip to content

Commit f48f087

Browse files
Refactor: move nodes_mask_convertion nodes to nodes_mask.
1 parent e1db7a2 commit f48f087

File tree

3 files changed

+31
-63
lines changed

3 files changed

+31
-63
lines changed

comfy_extras/nodes_mask.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,41 @@ def composite(self, destination, source, x, y, mask = None):
5959

6060
class MaskToImage:
6161
@classmethod
62-
def INPUT_TYPES(cls):
62+
def INPUT_TYPES(s):
6363
return {
64-
"required": {
65-
"mask": ("MASK",),
66-
}
64+
"required": {
65+
"mask": ("MASK",),
66+
}
6767
}
6868

6969
CATEGORY = "mask"
7070

7171
RETURN_TYPES = ("IMAGE",)
72+
FUNCTION = "mask_to_image"
73+
74+
def mask_to_image(self, mask):
75+
result = mask[None, :, :, None].expand(-1, -1, -1, 3)
76+
return (result,)
77+
78+
class ImageToMask:
79+
@classmethod
80+
def INPUT_TYPES(s):
81+
return {
82+
"required": {
83+
"image": ("IMAGE",),
84+
"channel": (["red", "green", "blue"],),
85+
}
86+
}
7287

73-
FUNCTION = "convert"
88+
CATEGORY = "mask"
7489

75-
def convert(self, mask):
76-
image = torch.cat([torch.reshape(mask.clone(), [1, mask.shape[0], mask.shape[1], 1,])] * 3, 3)
90+
RETURN_TYPES = ("MASK",)
91+
FUNCTION = "image_to_mask"
7792

78-
return (image,)
93+
def image_to_mask(self, image, channel):
94+
channels = ["red", "green", "blue"]
95+
mask = image[0, :, :, channels.index(channel)]
96+
return (mask,)
7997

8098
class SolidMask:
8199
@classmethod
@@ -231,10 +249,15 @@ def feather(self, mask, left, top, right, bottom):
231249
NODE_CLASS_MAPPINGS = {
232250
"LatentCompositeMasked": LatentCompositeMasked,
233251
"MaskToImage": MaskToImage,
252+
"ImageToMask": ImageToMask,
234253
"SolidMask": SolidMask,
235254
"InvertMask": InvertMask,
236255
"CropMask": CropMask,
237256
"MaskComposite": MaskComposite,
238257
"FeatherMask": FeatherMask,
239258
}
240259

260+
NODE_DISPLAY_NAME_MAPPINGS = {
261+
"ImageToMask": "Convert Image to Mask",
262+
"MaskToImage": "Convert Mask to Image",
263+
}

comfy_extras/nodes_mask_conversion.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

nodes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,4 +1193,3 @@ def init_custom_nodes():
11931193
load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_upscale_model.py"))
11941194
load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_post_processing.py"))
11951195
load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_mask.py"))
1196-
load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_mask_conversion.py"))

0 commit comments

Comments
 (0)