Skip to content

Commit b47eb8b

Browse files
committed
added the MaskofCenter node.
1 parent 5d6dc4c commit b47eb8b

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

py/nodes.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,96 @@ def analyze_faces(self, insightface, img_data: np.ndarray):
654654
print(f"\033[33mINFO: InsightFace detection resolution lowered to {size}.\033[0m")
655655
break
656656
return face
657+
658+
class MaskofCenter:
659+
@classmethod
660+
def INPUT_TYPES(s):
661+
return {
662+
"required": {
663+
"width": ("INT", {"default": 1024, "min": 0, "max": 8096, "step": 8}),
664+
"height": ("INT", {"default": 1024, "min": 0, "max": 8096, "step": 8}),
665+
"top": ("FLOAT", {"default": 0.25, "min": 0.0, "max": 1.0, "step": 0.01}),
666+
"bottom": ("FLOAT", {"default": 0.25, "min": 0.0, "max": 1.0, "step": 0.01}),
667+
"left": ("FLOAT", {"default": 0.25, "min": 0.0, "max": 1.0, "step": 0.01}),
668+
"right": ("FLOAT", {"default": 0.25, "min": 0.0, "max": 1.0, "step": 0.01}),
669+
"redius": ("FLOAT", {"default": 0.05, "min": 0.0, "max": 0.5, "step": 0.01}),
670+
},
671+
"optional": {
672+
"size_as": ("IMAGE",),
673+
}
674+
}
675+
676+
RETURN_TYPES = ("MASK",)
677+
FUNCTION = 'mask_get'
678+
CATEGORY = 'utils/mask'
679+
680+
def mask_get(self, size_as=None, width=1024, height=1024, top=0.25, bottom=0.25, left=0.25, right=0.25, redius=0.05):
681+
if size_as is not None:
682+
height, width = size_as.shape[-3:-1]
683+
684+
# 创建全白色mask
685+
mask = torch.ones((1, height, width), dtype=torch.float32)
686+
687+
# 计算各边界的像素值
688+
top_pixels = int(height * top)
689+
bottom_pixels = int(height * bottom)
690+
left_pixels = int(width * left)
691+
right_pixels = int(width * right)
692+
# 将边界区域设为黑色(0)
693+
if top > 0:
694+
mask[:, :top_pixels, :] = 0
695+
if bottom > 0:
696+
mask[:, -bottom_pixels:, :] = 0
697+
if left > 0:
698+
mask[:, :, :left_pixels] = 0
699+
if right > 0:
700+
mask[:, :, -right_pixels:] = 0
701+
702+
# 添加圆弧效果
703+
if redius > 0:
704+
mask = self.add_corner_radius(mask[0], top_pixels, bottom_pixels, left_pixels, right_pixels, redius)
705+
mask = torch.unsqueeze(mask, 0)
706+
707+
return (mask,)
708+
709+
def add_corner_radius(self, mask, top_pixels, bottom_pixels, left_pixels, right_pixels, radius_percent):
710+
"""在mask的白色区域四个角添加圆弧效果
711+
Args:
712+
mask: 2D tensor mask
713+
top_pixels: 顶部黑色区域高度
714+
bottom_pixels: 底部黑色区域高度
715+
left_pixels: 左侧黑色区域宽度
716+
right_pixels: 右侧黑色区域宽度
717+
radius_percent: 圆弧半径(相对于较短边的百分比)
718+
"""
719+
height, width = mask.shape
720+
min_side = min(height, width)
721+
radius = int(min_side * radius_percent)
722+
723+
if radius <= 0:
724+
return mask
725+
726+
# 创建圆形kernel
727+
y, x = torch.meshgrid(torch.arange(radius), torch.arange(radius))
728+
# 计算到圆心的距离
729+
dist_from_center = torch.sqrt((x - (radius-1))**2 + (y - (radius-1))**2).float()
730+
# 圆内为1,圆外为0
731+
circle = (dist_from_center <= radius).float()
732+
733+
# 左上角
734+
mask[top_pixels:top_pixels+radius, left_pixels:left_pixels+radius] = circle
735+
736+
# 右上角
737+
mask[top_pixels:top_pixels+radius, width-right_pixels-radius:width-right_pixels] = torch.flip(circle, [1])
738+
739+
# 左下角
740+
mask[height-bottom_pixels-radius:height-bottom_pixels, left_pixels:left_pixels+radius] = torch.flip(circle, [0])
741+
742+
# 右下角
743+
mask[height-bottom_pixels-radius:height-bottom_pixels, width-right_pixels-radius:width-right_pixels] = torch.flip(circle, [0, 1])
744+
745+
return mask
746+
657747
class MaskCoverFourCorners:
658748

659749
@classmethod
@@ -1245,6 +1335,7 @@ def forward(self, image, upscale_model, threshold_of_xl_area=0.9):
12451335
"MaskAutoSelector": MaskAutoSelector,
12461336
"MaskFromFaceModel": MaskFromFaceModel,
12471337
"MaskCoverFourCorners": MaskCoverFourCorners,
1338+
"MaskofCenter": MaskofCenter,
12481339

12491340
#loader
12501341
"CheckpointLoaderSimpleWithSwitch": CheckpointLoaderSimpleWithSwitch,
@@ -1281,6 +1372,7 @@ def forward(self, image, upscale_model, threshold_of_xl_area=0.9):
12811372
"MaskAutoSelector": "Mask Auto Selector",
12821373
"MaskFromFaceModel": "Mask from FaceModel",
12831374
"MaskCoverFourCorners": "Mask Cover Four Corners",
1375+
"MaskofCenter": "Mask of Center",
12841376

12851377
# Loader
12861378
"CheckpointLoaderSimpleWithSwitch": "Load Checkpoint with Switch",

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "comfyui-utils-nodes"
33
description = "Nodes:LoadImageWithSwitch, ImageBatchOneOrMore, ModifyTextGender, GenderControlOutput, ImageCompositeMaskedWithSwitch, ImageCompositeMaskedOneByOne, ColorCorrectOfUtils, SplitMask, MaskFastGrow, CheckpointLoaderSimpleWithSwitch, ImageResizeTo8x, MatchImageRatioToPreset, UpscaleImageWithModelIfNeed, MaskFromFaceModel, MaskCoverFourCorners, DetectorForNSFW, DeepfaceAnalyzeFaceAttributes etc."
4-
version = "1.1.9"
4+
version = "1.2.0"
55
license = { file = "LICENSE" }
66
dependencies = []
77

0 commit comments

Comments
 (0)