Nodes/tri3d-comfyui-nodes/Simple bg swap v5.1.0
ComfyUI Node

Simple bg swap v5.1.0

Paste a subject onto a new background and get a usable shadow layer for free

By TRI3D-LC·Created 3 years ago·Updated about a year ago· 27
Simple bg swap v5.1.0
  • bkg_image
  • subject_image
  • subject_mask
  • output bg swapped image
  • shadow layer
threshhold_hist150
scale_factor1.20
height_factor1.05

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:

  1. The subject is scaled and pasted onto the background (same bottom-center logic, driven by scale_factor and height_factor).
  2. 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.
  3. 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_hist to shrink it.
  • The background gets rescaled - same as scaled-paste: output dimensions follow scale_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 Islands or 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.

CategoryTRI3D

Inputs (6)

NameTypeDefaultDescription
bkg_imageIMAGE
subject_imageIMAGE
subject_maskMASK
threshhold_histINT1500–255
scale_factorFLOAT1.200–10
height_factorFLOAT1.051–8

Outputs (2)

NameTypeDescription
output bg swapped imageIMAGE
shadow layerMASK