Nodes/Image Misc/Apply Mask using AFFCE
ComfyUI Node

Apply Mask using AFFCE

Apply a background-removal mask without the edge halo

By set-soft·Created about a year ago·Updated 8 months ago· 2
Apply Mask using AFFCE
  • images
  • masks
  • image
  • mask
blur_size90
blur_size_two6
fill_colorfalse
color#000000
batchedtrue

You've got a mask from a background-removal model and you want to actually use it - cut the background out and get a transparent PNG, or drop the subject on a new background. Naively multiplying the image by the mask is how you get those ugly hard-edged cutouts with background color smeared along the hairline. This node exists so you don't do that. It's the "apply this mask properly" step, and it does the job by running AFFCE (Approximate Fast Foreground Colour Estimation) on your image + mask, then compositing the result onto transparency or a color.

If that sounds like the Estimate foreground (AFFCE) node with extra steps - you're half right. The sibling node only estimates the clean foreground and hands it back to you for manual compositing. This one packages the estimation and the cutout into a single image output, which is what you actually want 90% of the time. The AFFCE engine is the same: a two-pass blur-and-fuse algorithm, ported to pure PyTorch from the Photoroom fast-foreground-estimation work (via BiRefNet's image processing code), that recovers the true foreground color where the pixels are a mix of subject and background.

The inputs that matter

  • images - the subject on its original background.
  • masks - the alpha matte telling it what's foreground and what's background.
  • fill_color - the big decision. Off (default): you get an RGBA image with the background made transparent. On: the background is replaced by color and you get a plain RGB image. Turn it on if a downstream node can't handle alpha.
  • color - the fill color when fill_color is on; hex #RRGGBB or comma-separated RGB in 0–255 or 0.0–1.0.
  • blur_size / blur_size_two - the coarse and fine blur diameters driving the estimation. Defaults (90 and 6) work for most photos; raise the first for very soft edges, drop the second for more detail.
  • batched - process the whole batch at once (faster, more VRAM) or one image at a time.

Outputs are image and mask. The mask output is just your input mask echoed back - handy for keeping the pipeline tidy, not a new result.

Installing it

Same pack, same drill. ComfyUI Manager → "Image Misc", or:

cd ComfyUI/custom_nodes
git clone https://github.com/set-soft/ComfyUI-ImageMisc
cd ComfyUI-ImageMisc && pip install -r requirements.txt

Restart, find it under image/manipulation. No model downloads - it's pure PyTorch on top of ComfyUI's stack, which is the pack's whole point.

Where it falls short

AFFCE is fast and free, but it's approximate. On semi-transparent material - glass, veils, smoke - no blur-and-fuse pass can reconstruct detail that the original image genuinely blended away; for those you want the pack's FMLFE node (a real multi-level solver that also produces a background plate) or a matting-specific model. And remember this node does not sharpen your mask: feed it a blurry, noisy mask and you'll get the halo gone but the fuzziness baked in. Clean masks in, clean cutouts out. For the common case - a decent BiRefNet-style mask on a portrait - this is the one-step node that just works, and it costs nothing to run.

Categoryimage/manipulation

Inputs (7)

NameTypeDefaultDescription
imagesIMAGE
masksMASK
blur_sizeINT901–255
blur_size_twoINT61–255
fill_colorBOOLEANfalseFill the background using a color. Returns an RGB image, otherwise an RGBA.
colorSTRING#000000Color for fill. Can be an hexadecimal (#RRGGBB). Can comma separated RGB values in [0-255] or [0-1.0] range.
batchedBOOLEANtrueProcess the images at once. Faster, needs more memory

Outputs (2)

NameTypeDescription
imageIMAGE
maskMASK