Nodes/ComfyUI_Swwan/Image Matte (Swwan)
ComfyUI Node

Image Matte (Swwan)

Mask in, transparent PNG out — and no, it doesn't detect anything

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
Image Matte (Swwan)
  • image
  • mask
  • background_color
  • rgba
  • rgb
  • mask
◄fill_holestrue►
◄crop_masktrue►
◄crop_factor1.2►
◄stroke_width0►

The author's own description is unusually blunt about the scope, which is refreshing: "Use an existing mask as alpha; optionally fill holes, crop and add a stroke. RGB fills transparent pixels with background_color. This does not run background detection or premultiply RGB."

Read that twice, because "matte" in a background-removal context usually means rembg or BiRefNet - a model that figures out the subject for you. This node doesn't do that. It's the last step of that pipeline, not a replacement for the first one. You bring the mask; it does the compositing craft.

That's a real distinction worth having, because the detection step is the one people swap out every six months (SAM, BiRefNet, RMBG, whatever shipped this quarter) and the alpha handling is stable, fiddly work you'd rather not redo per model.

Inputs and outputs

Required: image (IMAGE) and mask (MASK).

Optional: fill_holes (BOOLEAN, default true), crop_mask (BOOLEAN, default true), crop_factor (FLOAT, default 1.2, range 1.0–2.0, step 0.1), stroke_width (INT, default 0, 0–512), background_color (COLORCODE, default #364254).

Three outputs: rgba (IMAGE with alpha kept), rgb (IMAGE with transparent pixels filled), mask (MASK).

What each option does

fill_holes runs a binary hole fill on the mask before anything else - interior gaps in a subject get closed, so an imperfect segmentation doesn't punch holes through a face. It uses scipy where available and falls back to a simple flood-fill implementation if not installed (requirements.txt installs scipy, so in practice you get the good path).

crop_mask cuts the canvas down to the subject's bounding box, expanded by crop_factor - 1.2 means 20% of the subject's width and height added around it. With stroke_width greater than zero, the expansion also accounts for the stroke so the outline never gets clipped. This is the difference between a 1024×1024 canvas with a small figure in it and a tight, ready-to-place cutout.

stroke_width draws an outline, in background_color, around the matte. That's the sticker-border look. Note the colour coupling: the stroke and the RGB fill are the same colour here, so you can't have a white outline and a black fill from one node - pick one and do the other with a separate node if you need both.

background_color fills everything transparent in the rgb output. Default #364254 is a dark slate rather than black, which flatters cutouts and edge halos. If you want a true black or white plate, set it explicitly.

The masking convention trap

The mask here is alpha-like: bright means keep, dark means transparent. That is the opposite of what a lot of ComfyUI mask flows hand you from the start, because Load Image's MASK output is 1 - alpha - it inverts the alpha channel. The pack's own RGBA documentation calls this out explicitly as a standard place to get burned, and it's the single most common "this node inverted my image" report.

So if your output is a hole where the subject should be, invert your mask before this node - or check whether you're passing a mask that came off Load Image's alpha channel, which is already inverted relative to what you wanted.

Also flag: since the node does not premultiply, rgba is straight alpha. Compositing straight-alpha RGBA onto an RGB background elsewhere can leave bright fringes; that's why the pack ships a whole RGBA Safe Pre/Post/Save trio for passing alpha through RGB-only pipelines.

Finally, the mask output is the processed mask - after hole filling, cropping and stroke - which is what you want for a downstream composite that has to match the cutout exactly. Feed that, not your original mask, or the edges won't line up.

Install

cd /path/to/ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan.git
cd ComfyUI_Swwan
python -m pip install -r requirements.txt

Windows portable:

.\python_embeded\python.exe -m pip install .\ComfyUI\custom_nodes\ComfyUI_Swwan\requirements.txt

Restart ComfyUI, hard-refresh the browser, search Swwan; it's under Swwan/Advanced/Image. No detection models, no model downloads at all - bring your own mask from a segmenter, a hand-drawn mask, or a chroma key. opencv-python and scipy from requirements.txt cover the pixel work; torch and torchvision come from your ComfyUI environment.

CategorySwwan/Advanced/Image

Inputs (7)

NameTypeDefaultDescription
imageIMAGE—
maskMASK—
fill_holesoptBOOLEANtrue—
crop_maskoptBOOLEANtrue—
crop_factoroptFLOAT1.21–2—
stroke_widthoptINT00–512—
background_coloroptCOLORCODE#364254—

Outputs (3)

NameTypeDescription
rgbaIMAGE—
rgbIMAGE—
maskMASK—