Skip to content

Commit e800187

Browse files
committed
add the ImageMaskColorAverage node.
1 parent a42c2c6 commit e800187

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ This node is designed to generate a transition image between two images. The fir
107107
## ImageTransitionLeftToRight
108108
This node is designed to generate a transition image between two images. The first image gradually slides to the right while the second image simultaneously appears from the left, creating a smooth transition effect.
109109

110+
## ImageMaskColorAverage
111+
This node is designed to calculate the average color of the image within the mask. It returns the decimal and hexadecimal values of the average color.
112+
110113
## TorchCompileModelAdvanced
111114
This node enables model compilation using torch.compile. It extends ComfyUI's original torch compile node by adding compile mode options and a toggle switch.
112115

py/node_image.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,67 @@ def generate_transition(self, first_image, last_image, frames, transition_type="
101101
result = torch.cat(output_frames, dim=0)
102102

103103
return (result,)
104+
105+
class ImageMaskColorAverage:
106+
@classmethod
107+
def INPUT_TYPES(s):
108+
return {
109+
"required": {
110+
"image": ("IMAGE",),
111+
"mask": ("MASK",),
112+
}
113+
}
104114

105-
NODE_CLASS_MAPPINGS = {
115+
RETURN_TYPES = ("INT", "STRING")
116+
RETURN_NAMES = ("COLOR_DEC", "COLOR_HEX")
117+
FUNCTION = "calculate_average_color"
118+
CATEGORY = "utils/image"
106119

120+
def calculate_average_color(self, image, mask):
121+
# 确保mask是二维的
122+
if len(mask.shape) > 2:
123+
mask = mask.squeeze()
124+
125+
# 将mask扩展为与图像相同的通道数
126+
expanded_mask = mask.unsqueeze(-1).expand(-1, -1, 3)
127+
128+
# 计算mask区域的像素总数
129+
pixel_count = torch.sum(mask)
130+
131+
if pixel_count == 0:
132+
# 如果mask中没有选中区域,返回黑色
133+
return (0, "#000000")
134+
135+
# 计算mask区域的颜色总和
136+
masked_image = image * expanded_mask.unsqueeze(0)
137+
color_sum = torch.sum(masked_image, dim=[0, 1, 2])
138+
139+
# 计算平均颜色
140+
avg_color = color_sum / pixel_count
141+
142+
# 转换为0-255范围的整数
143+
r = int(avg_color[0].item() * 255)
144+
g = int(avg_color[1].item() * 255)
145+
b = int(avg_color[2].item() * 255)
146+
147+
# 生成十六进制颜色代码
148+
hex_color = f"#{r:02x}{g:02x}{b:02x}"
149+
150+
# 计算十进制颜色值 (R*65536 + G*256 + B)
151+
dec_color = r * 65536 + g * 256 + b
152+
153+
return (dec_color, hex_color)
154+
155+
NODE_CLASS_MAPPINGS = {
107156
#image
108157
"ImageCompositeWatermark": ImageCompositeWatermark,
109158
"ImageTransition": ImageTransition,
159+
"ImageMaskColorAverage": ImageMaskColorAverage,
110160
}
111161

112162
NODE_DISPLAY_NAME_MAPPINGS = {
113163
# Image
114164
"ImageCompositeWatermark": "Image Composite Watermark",
115165
"ImageTransition": "Image Transition",
166+
"ImageMaskColorAverage": "Image Mask Color Average",
116167
}

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.2.8"
4+
version = "1.2.9"
55
license = { file = "LICENSE" }
66
dependencies = []
77

0 commit comments

Comments
 (0)