Simple bg swap v5.1.0
Paste a subject onto a new background and get a usable shadow layer for free
- bkg_image
- subject_image
- subject_mask
- output bg swapped image
- shadow layer
Every beginner asks "how do I put my person on a new background?" and the naive answer - paste the cutout onto the photo - always looks wrong. Flat subjects glued onto scenes read as cutouts. This node is the answer that looks right: it does the scaled paste and derives a shadow layer from the subject's own luminance, so the result has a grounded, shaded look instead of a sticker-on-glass one.
The two outputs are the point. You get the composited image, and you also get a shadow layer MASK - the thing most paste tools make you fake by hand. That mask is what makes the swap read as "this person is actually standing there."
How it works
It's built on the pack's scaled-paste idea, extended in three passes:
- The subject is scaled and pasted onto the background (same bottom-center logic, driven by
scale_factorandheight_factor). - A second paste renders the subject onto pure white, and the L (lightness) channel of that white-plate render is inverted and remapped. Where the subject is bright on white, the result is dark - i.e., the shadow the subject would cast.
- That shadow map is masked to the subject's footprint, then multiplied against the composited image to darken it, producing a shaded, grounded result.
The threshhold_hist slider (0–255, default 150) controls how aggressively the lightness map gets stretched before it becomes a shadow - lower values darken more, higher values keep it subtle. The shadow layer output gives you that map as a MASK to use or edit however you like.
The inputs that matter
bkg_image(IMAGE) - the new scene.subject_image(IMAGE) - the person/object.subject_mask(MASK) - the subject's cutout.threshhold_hist(INT, 0–255, default 150) - shadow strength/edge of the darkening.scale_factor(FLOAT, 0–10, default 1.2) - subject size.height_factor(FLOAT, 1–8, default 1.05) - vertical placement.
Outputs: output bg swapped image (IMAGE) and shadow layer (MASK).
Installing it
Same pack:
cd ComfyUI/custom_nodes
git clone https://github.com/TRI3D-LC/tri3d-comfyui-nodes
cd tri3d-comfyui-nodes
pip install -r requirements.txt
or ComfyUI Manager → "tri3d-comfyui-nodes", restart, under TRI3D. The pack also ships a sibling node, tri3d-get_threshold_for_bg_swap, which estimates a good threshhold_hist value automatically from the subject image.
Common issues
- Shadow looks like a dark halo - the shadow is derived from the subject's own lightness, so a bright subject on a white plate produces a wide dark band. Tighten the mask and lower
threshhold_histto shrink it. - The background gets rescaled - same as
scaled-paste: output dimensions followscale_factor. Plan a resize downstream if you need a fixed size. - Mask edges still show - the shadow pass can't fix a bad cutout; clean the mask with the pack's
Remove Small Mask Islandsor a feather before swapping. - DEBUG spam - this family of nodes prints debug lines on every run. Expected, harmless.
For a one-node background swap with decent visual grounding, this is probably the best single stop in the pack - you'd otherwise need a paste node, a shadow hack, and two composite nodes to get the same result.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| bkg_image | IMAGE | — | |
| subject_image | IMAGE | — | |
| subject_mask | MASK | — | |
| threshhold_hist | INT | 1500–255 | — |
| scale_factor | FLOAT | 1.200–10 | — |
| height_factor | FLOAT | 1.051–8 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| output bg swapped image | IMAGE | — |
| shadow layer | MASK | — |