Composite
Foreground, background, and a mask
- foregrounds
- backgrounds
- foreground_masks
- IMAGE
Removing a background is only half the job. The other half is putting the subject back down somewhere else, and that's what this node is for: it takes your foreground image, your background image, and your foreground mask, and merges them into one output. It's the workhorse of yondonfu's ComfyUI-Background-Edit pack, and it's what makes both background swapping and depth-based background blur actually produce a final image.
The neat part is where it's designed to live. This pack is built for real-time video via comfystream (same author), and Composite is one of the few nodes there that runs comfortably on GPU per frame. Mask generation is the expensive step; compositing is a couple of tensor multiplies.
How it works
Straightforward alpha blending, written out as math instead of using an alpha channel:
result = foreground * mask + background * (1 - mask)
The mask is expanded from [B,H,W] to [B,H,W,1] so it multiplies across the RGB channels. A hard mask (all 0s and 1s) gives you a clean cut; a soft, anti-aliased mask gives you actual edge blending, which is what makes a cutout not look glued on. It expects the mask's polarity to be foreground-white/background-black and computes the inverse internally - flip that polarity and your subject quietly disappears under the background.
There's no model here at all, and that's deliberate. The pack's README shows it fed by DepthAnything: kijai's ComfyUI-DepthAnythingV2 produces a depth map, you threshold it into a foreground mask, and Composite drops the sharp subject over a GaussianBlur-softened version of the same frame for a real-time defocus effect.
The inputs that matter
foregrounds(IMAGE) - the subject you're keeping.backgrounds(IMAGE) - what goes behind it (a blurred copy, aBackgroundColorfill, or any image).foreground_masks(MASK) - the white/black separation mask.mode-cudaorcpu, defaultcuda. This one bites people. Leave it oncudawith no CUDA GPU and the node throws when it tries to move tensors over; flip it tocpuand it runs anywhere.
Output is a single IMAGE - wire it to a preview, a save node, or a VHS/video encoder for streaming.
Install
Same steps as the rest of the pack - ComfyUI Manager (search "ComfyUI-Background-Edit"), or:
cd ComfyUI/custom_nodes
git clone https://github.com/yondonfu/ComfyUI-Background-Edit
cd ComfyUI-Background-Edit
pip install -r requirements.txt
Its only Python dependencies are torch and torchvision, so there are no model downloads and nothing heavy to configure. The models that make it useful (DepthAnything) are separate packs the README lists as prerequisites - the image workflows expect ComfyUI-DepthAnythingV2, the real-time workflow wants the TensorRT-accelerated version.
Where people get burned
Three real failure modes, all visible in the source:
mode: cudaon a CPU-only box. The default crashes instantly. If you're on Windows without a working CUDA build, setcpu.- Batch count mismatch. The node checks that foregrounds, backgrounds, and masks all have the same number of frames and raises
mismatch number of backgrounds, foregrounds and foreground masks. Feeding a batched foreground into a single-frame background is the classic cause - run them through the same LoadImage/VHS path. - Resolution mismatch. There's no automatic resize. If the tensors have different spatial dimensions you get a torch broadcast error, not a friendly message. Resize before compositing, not after.
For real-time video, remember that a noisy per-frame mask makes the subject edges flicker - that's the mask, not this node. Composite is the cheap, dependable last step; if the output looks wrong, suspect what you're feeding it.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| foregrounds | IMAGE | — | |
| backgrounds | IMAGE | — | |
| foreground_masks | MASK | — | |
| mode | COMBO | cuda | 2 options: cuda, cpu |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |