遮罩生成背景图
Give It a Mask, Get White or Transparent Background — Same Composition
- image
- masks
- image
You've got a product mask - now what? KSApplyMaskBackground is the compositing step: it takes your image, keeps the foreground defined by one or more masks, and outputs the same composition with everything else turned white or transparent. Crucially, the framing doesn't change. This isn't a crop or a cutout; it's "original photo, background replaced." That's the step that turns a masked product shot into a clean listing-ready image without losing the scene.
How it works
Feed it an image, a mask (or several - they're merged into one union mask), and a background_mode: white gives an RGB image on white; transparent gives an RGBA image with a real alpha channel. The output stays at the input's size and composition. Along the way the node runs the mask through a small cleanup chain that makes a big practical difference:
mask_threshold(0.5) - binarize: lower keeps more soft edge, higher tightens the subject.min_mask_component_area(64) - drop specks smaller than this.mask_close_radius(4) - close small gaps and smooth the outline.mask_expand_pixels(2) - grow the mask outward so you don't clip hair or fuzzy edges.fill_mask_holes(on) - fill interior holes; turn off for lace or see-through structures.edge_feather(0.8) - soften the boundary so the composite doesn't look pasted-on.
For solid products, the defaults are genuinely good. For a hollow or transparent product (a hanger, a glass bottle), flip fill_mask_holes off and lower edge_feather to keep the structure honest.
One nice behavior: if your image is a batch that matches the mask count, the node composites each masked instance onto the same canvas. So multiple detected products from the SAM step can all land on one white-background image - that's the "keep them together" composition mode, as opposed to KSProductsToBackground, which splits them into separate squares.
Installing
With the pack:
cd ComfyUI/custom_nodes
git clone https://github.com/kongshan4219/ComfyUI-Kongshan-Nodes
restart ComfyUI. Purely local, no models, no keys - just numpy/Pillow.
Gotchas
- Garbage in, garbage out - the mask is the whole game. This node applies a mask; it doesn't refine a bad one beyond the mechanical cleanup. If your SAM mask is sloppy, the background composite will be sloppy. Get the mask right upstream (that's what the feather/expand settings partly compensate for).
transparentmode preserves alpha only in the output tensor. If you save through a JPEG pipeline later, the transparency is gone - the pack's ownKSDirectorySaveImageshandles PNG properly for exactly this reason.- Empty image or mask list raises. "Empty image or masks list received." is the literal error; usually means a mask batch and image batch that don't line up.
Context from the KB's background-removal essay: post-generation background replacement is the most commoditized job in the ecosystem, and this node is squarely in that family - except it's driven by your mask, not by a background-removal model. That's the difference that matters: SAM masks follow the product exactly, while automatic background removers guess. For catalog work where the product silhouette is the whole point, the mask path wins.
Inputs (9)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | 原始图片或分割图片批次。输入为分割图片批次时,会按对应 mask 逐张合成到同一画布。 | |
| masks | MASK | 一个或多个前景遮罩。多个遮罩会合并为一个 union mask。 | |
| background_mode | COMBO | 背景模式。white 输出白底 RGB 图;transparent 输出带 alpha 通道的透明底图。 | |
| edge_featheropt | FLOAT | 0.80–8 | 遮罩边缘羽化半径。0 为硬边;数值越大边缘越柔和。 |
| mask_thresholdopt | FLOAT | 0.500.05–0.95 | 遮罩二值化阈值。降低保留更多边缘;提高会收紧主体。 |
| min_mask_component_areaopt | INT | 640–4096 | 最小连通区域面积。0 不过滤;值越大越能去噪,也越可能丢失小细节。 |
| mask_close_radiusopt | INT | 40–32 | 遮罩闭运算半径。用于连接小缝隙和平滑边缘;过大会扩张或粘连主体。 |
| mask_expand_pixelsopt | INT | 20–32 | 遮罩向外扩展像素数。提高可避免边缘被裁掉,但可能保留少量背景。 |
| fill_mask_holesopt | BOOLEAN | true | 是否填充遮罩内部空洞。开启适合实心商品;关闭适合保留镂空或透明结构。 |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |