ComfyUI Node

背景移除

Background Removal, No API Key Required

By assemly·Created about a year ago·Updated about a year ago· 0
背景移除
  • images
  • IMAGE
  • MASK
model_nameu2net
alpha_mattingfalse
alpha_matting_foreground_threshold240
alpha_matting_background_threshold10
alpha_matting_erode_size10

The name is a lie in the best way

BGRemover (shown in ComfyUI as "背景移除") doesn't call some paid background-removal API and it doesn't need a key. It's a thin local wrapper around rembg - the same MIT library that powers half the "free background remover" web apps you've seen - so it runs entirely on your machine. Feed it an image, get back an RGBA image with the background knocked out plus a mask. That's the whole job, and for a solid subject against a contrasting background it's been effectively solved since 2020.

That last sentence sets expectations, so hold onto it. This node's model list is the u2net family: u2net (default), u2netp, u2net_human_seg, u2net_cloth_seg, silueta, and isnet-general-use. These are segmentation models - they label each pixel foreground or background. Fine for products and people, visibly shaky on flyaway hair, fur, veils, or anything genuinely semi-transparent. If your input is hard-edged, this is the fast, free, no-drama answer. If you're fighting hair, the community answer is BiRefNet or InSPyReNet, and this pack doesn't offer those - its dropdown is frozen at the six above.

How it works

The node wraps two rembg calls: new_session(model_name) loads the selected model, then remove() runs it over every image in your batch. Each tensor becomes a PIL image, the background gets cut, and the alpha channel comes back as the MASK output. The IMAGE output keeps that alpha, so it's genuinely transparent RGBA, not a fake-out. On error it quietly returns the original image and an empty black mask instead of crashing - which is worth knowing the first time you see a black MASK and wonder what died.

The inputs you'll actually touch

  • images - required, an IMAGE. It batches fine, so whole folders go through one run.
  • model_name - the dropdown that matters. u2netp for speed (slightly less precise), u2net_human_seg for portraits, u2net_cloth_seg for clothing shots. Default u2net is the balanced pick.
  • alpha_matting - off by default. Flipping it on runs pymatting to refine edges into fractional alpha, which helps hair and fuzzy edges at a real speed cost. Only turn it on when the plain cutout looks wrong.
  • The three thresholds (foreground_threshold 240, background_threshold 10, erode_size 10) are rembg's matting knobs. The defaults are sane. Don't touch them until the matte looks too tight or too loose.

Outputs are IMAGE (RGBA) and MASK (the alpha). One gotcha straight from the README: you can't wire MASK into a PreviewImage node directly. Route it through ComfyUI's built-in MaskToImage first if you want to see it.

Installing it

The easy way is ComfyUI Manager - search "comfyui-bgremover" (or the Chinese display names; the pack is authored in Chinese, so the node titles won't match an English search). Manual install is the usual two steps:

cd ComfyUI/custom_nodes
git clone https://github.com/assemly/comfyui-bgremover.git
cd comfyui-bgremover
pip install -r requirements.txt

Then restart ComfyUI. There's also a python install.py that runs the pip step for you. The heavy dependency is rembg itself, which drags in onnxruntime, pymatting, and pooch. No model weights ship in the repo - rembg downloads them on first use into ~/.u2net (roughly a 175MB file). So the first run of the node can stall while it fetches the weights; a flaky connection or corporate proxy is the classic "it just hangs" cause. Let it finish once and it's cached forever.

Where people get burned

  • First-run download. See above. If the first run fails, just re-run - the network is the fragile part, not the node.
  • Windows + CUDA. The README's own tip: make sure your CUDA toolkit matches what onnxruntime expects. If CPU inference feels slow on Windows, pip install onnxruntime-gpu and confirm the runtime picks up your GPU.
  • It always re-runs. The node marks itself as always-changed, so it re-segments on every queue run with no caching. Usually harmless; a surprise when you're iterating over a big batch.
  • Silent fallback. A black MASK next to an unchanged image means the segmentation threw an error, not that the image has no background.

Honest verdict: a capable, zero-config rembg node for the easy 80% of cutouts. For hard edges, keep a BiRefNet or InSPyReNet node in the graph. For everything else - products, portraits on clean backdrops, batch extraction - this is the one that just works.

Categoryimage/preprocessors

Inputs (6)

NameTypeDefaultDescription
imagesIMAGE
model_nameoptCOMBOu2net6 options: u2net, u2netp, u2net_human_seg, u2net_cloth_seg, silueta, isnet-general-use
alpha_mattingoptBOOLEANfalse
alpha_matting_foreground_thresholdoptINT2400–255
alpha_matting_background_thresholdoptINT100–255
alpha_matting_erode_sizeoptINT100–1000

Outputs (2)

NameTypeDescription
IMAGEIMAGE
MASKMASK