🎨 智绘_背景替换
Composite a cutout onto a new background with three blend modes
- foreground
- mask
- background
- 合成图像
Cutting a subject out is only half the job. The other half is putting it somewhere else, and that's where composites fall apart: the subject has clean edges, the new background is gorgeous, and the seam between them screams. ZH_BackgroundReplacer (🎨 智绘_背景替换) is the small node in the 智绘灵箱 pack that does the compositing half - foreground + mask + background in, one composited image out, with a feather control to kill the seam. It's designed to sit directly after the pack's own ZH_BackgroundRemover, which hands you exactly the IMAGE and MASK sockets this node wants.
How it works
It's the classic alpha-composite math, and it's transparent about it. The mask is normalized to match your foreground, resized to the foreground's dimensions if the background differs (bilinear, so mismatched sizes just work), then the two images are blended per-pixel by the mask:
- 正常覆盖 (Normal, the default) -
foreground × mask + background × (1 − mask). The standard cut-and-paste. - 正片叠底 (Multiply) - darkens where the layers overlap. Good for lighting/shadow effects or making a subject feel embedded in a darker scene.
- 滤色 (Screen) - lightens. The "glow in the scene" mode for bright subjects or additive effects.
The one quality control is feather_edge (0–50): it Gaussian-blurs the mask before compositing, and this is the slider that decides whether your composite looks placed-on-top or photographed-together. It uses scipy.ndimage for the blur - which is why scipy is in the pack's requirements file, not just as decoration. A feather of 2–5px is usually enough to soften a decent mask without eating detail; push higher only for deliberately soft edges.
There's a small batch-smartness worth knowing: if the background is a single image and the foreground is a batch, the background is tiled across the whole batch automatically. So you can cut out 20 product shots and drop them all on one studio backdrop in a single pass.
The inputs that matter
Three inputs are the whole show: foreground (the cutout image), mask (its alpha, from ZH_BackgroundRemover's 遮罩 socket - or any MASK source), and background (the new backdrop). Then blend_mode and feather_edge as above. Output is a single 合成图像 (IMAGE), ready for your save or further processing.
Install
Part of the 智绘灵箱 (ComfyUI-ZhiHui) pack:
cd ComfyUI/custom_nodes
git clone https://github.com/zhuyungen/ComfyUI-ZhiHui.git
Restart ComfyUI (or ComfyUI Manager, "智绘灵箱" / "ComfyUI-ZhiHui"). Runtime deps are torch + scipy, both in the pack's requirements.
Where people get burned
- The seam isn't the node's fault - it's the mask's. If your cutout has a hard, aliased edge, no amount of feathering fixes it. Go back to the remover and enable Alpha Matting, or switch to a BiRefNet weight. Garbage mask in, garbage composite out - this node is honest plumbing, not a miracle worker.
- Foreground and background light don't match. The node blends pixels, it doesn't match color grade or cast shadows. A subject shot under cool light pasted on a warm studio backdrop will still look pasted. Match your sources in the generator or with
ZH_ColorAdjustAdvancedfirst. - Sizes auto-resize, but that's a convenience with a price. A 2048px subject onto a 512px background gets downscaled silently. Keep the background at or above foreground resolution to avoid surprises.
- Feather is global. One number for the whole mask, so if your subject has both a crisp edge (product) and a fuzzy one (hair), you'll have to pick a compromise - or feather in a dedicated mask node instead.
It's a boring, dependable node, and that's the compliment. For a "remove then replace" workflow inside one pack, it closes the loop without making you drag in a full compositing suite.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| foreground | IMAGE | — | |
| mask | MASK | — | |
| background | IMAGE | — | |
| blend_mode | COMBO | 正常覆盖 (推荐) | 3 options: 正常覆盖 (推荐), 正片叠底 (变暗), 滤色 (变亮) |
| feather_edge | INT | 00–50 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| 合成图像 | IMAGE | — |