Nodes/quadmoon's ComfyUI nodes/Change Background of Image (SEGM SEGS)
ComfyUI Node

Change Background of Image (SEGM SEGS)

Swap a Photo's Background With a Two-Pass Inpainting Sampler

By traugdor·Created 3 years ago·Updated 9 months ago· 16
Change Background of Image (SEGM SEGS)
  • model
  • model_2
  • positive
  • positive_2
  • negative
  • negative_2
  • image
  • vae
  • segs_from_SEGM_Detector
  • IMAGE
seed0
steps20
steps_212
cfg8.0
sampler_name
scheduler
denoise1.00
denoise_20.40

Take a photo of a person, keep the person, generate a brand-new background around them - that's this node. It's not a background-removal tool in the BiRefNet sense (see the KB essay on why you should pick your cutout model by failure mode); it's a generative background replacer. It decides what's foreground and what's background from a mask, regenerates the background with a sampler, then runs a second pass over the whole image to blend the seam. Two passes, two checkpoints, one output.

Why you'd reach for it

When you want the subject preserved exactly and the scene around them reinvented - putting a portrait on a beach, dropping a product onto a studio backdrop. The key difference from compositing: the new background is generated, so it's lit and textured by the same model that made the subject, which makes it blend far better than pasting a cutout onto a stock photo. The KB's inpainting essay is relevant context here: this is the "mask still wins" case where you want the subject pixels untouched, which is exactly what a masked inpaint pass guarantees and what edit-models can't.

How it works

It's an inpainting sampler built on Impact Pack's output. You feed it SEGS from Impact Pack's SEGM Detector (the node that finds the person/object and produces a mask), and the node builds a mask from those segs - the code is literally borrowed from Impact Pack for compatibility. That mask is inverted (background becomes the region to regenerate), grown a few pixels so the latent-space seam stays clean, and used as the noise mask for a first sampling pass that fills in the background according to your positive/negative. Then the whole image is resampled with the second model and conditioning at low denoise (default 0.4) and no mask, which is what blends everything together.

Inputs and outputs

The long input list is really three groups:

  • Who does what: model/positive/negative drive the background pass; model_2/positive_2/negative_2 drive the blend pass. They can be the same model - most people use one checkpoint for both and just write a background description in positive and a whole-scene description in positive_2.
  • The subject: image (your photo), vae (for encode/decode), and segs_from_SEGM_Detector (the mask, from Impact Pack's SEGM Detector).
  • The dials: seed, steps (20), steps_2 (12), cfg (8), sampler_name, scheduler, denoise (1.0) and denoise_2 (0.4).

One output: IMAGE, the finished composite.

Installing it

Part of "quadmoon's ComfyUI nodes", but it has a real dependency: Impact Pack must be installed for the SEGS input to exist. Install both:

cd ComfyUI/custom_nodes
git clone https://github.com/traugdor/ComfyUI-quadMoons-nodes.git
git clone https://github.com/ltdrdata/ComfyUI-Impact-Pack.git

Or use ComfyUI Manager for both (search "quadmoon's ComfyUI nodes" and "Impact Pack"). Restart ComfyUI. No extra model downloads for the node itself - though the SEGM detector may want its own detection model, which Impact Pack guides you through.

Common issues

The usual failure is the seam: if you see a hard edge around the subject, denoise_2 is doing too little work - nudge it up toward 0.5. Crank it too far and you get a subject that drifts from the original, because the second pass is un-masked and re-draws everything. Also, your input image dimensions should be multiples of the VAE's downscale factor or the node crops a few pixels off an edge (it handles this internally, but it's why a 512x512-friendly input is safest).

CategoryQuadmoonNodes/sampling

Inputs (17)

NameTypeDefaultDescription
modelMODEL
model_2MODEL
positiveCONDITIONING
positive_2CONDITIONING
negativeCONDITIONING
negative_2CONDITIONING
imageIMAGE
vaeVAE
segs_from_SEGM_DetectorSEGS
seedINT00–18446744073709550000
stepsINT201–10000
steps_2INT121–10000
cfgFLOAT8.00–100
sampler_nameCOMBO44 options: euler, euler_cfg_pp, euler_ancestral, euler_ancestral_cfg_pp, heun, heunpp2, +38
schedulerCOMBO9 options: simple, sgm_uniform, karras, exponential, ddim_uniform, beta, +3
denoiseFLOAT1.000–1
denoise_2FLOAT0.400–1

Outputs (1)

NameTypeDescription
IMAGEIMAGE