💀Image Mask Expand
Grow or shrink a mask without the jagged edges
- mask
- processed_mask
Mask expanding is one of those operations every ComfyUI user needs and few nodes do well. You've got a segmentation mask that hugs the subject too tight - a hair's width of unselected edge - and you want to push it out a few pixels. Most implementations dilate the mask with a box kernel and hand you back a chunky, pixelated outline that ruins the feather. 💀Image Mask Expand from the S4Tool-Image pack does it properly: it treats the mask as a signed distance field and re-thresholds it, which gives you a smooth, anti-aliased edge whether you're growing or shrinking.
That's the mechanism worth understanding, because it's why this node behaves better than the naive ones. Dilation by brute force adds a hard pixel border. Here the mask is turned into a distance function (positive inside the shape, negative outside), shifted by your pixel amount, and thresholded with a small soft transition. Net effect: expand_px = +5 pushes the edge out 5px while keeping a feathered boundary instead of a staircase.
Inputs and output
mask- a MASK tensor.expand_px(−100 to +100, default 0) - the whole node. Positive values expand/dilate, negative values contract/erode. Default 0 is a pass-through.
One output: processed_mask (MASK), ready to feed an inpaint, a compositor, or another mask node. Note it's mask-in, mask-out - no image involvement, so it chains cleanly between whatever produces a mask and whatever consumes it.
Where you'll actually use it: after a 💀Image SAM / SAM2 segmentation when the selection is a couple pixels tight, before feeding a mask into a detail-inpaint pass so the model has room to blend, or shrinking a mask to kill a background halo. In an inpaint loop, a small positive expand is often the difference between a clean edit and a hard seam.
Install
It ships with the S4Tool-Image pack:
cd ComfyUI/custom_nodes/
git clone https://github.com/S4MUEL-404/ComfyUI-S4Tool-Image.git
pip install -r ComfyUI-S4Tool-Image/requirements.txt
Or ComfyUI Manager → search "S4Tool-Image" → Install → restart.
Nothing to download - this one only needs numpy and opencv, both in the pack's requirements.
Gotchas
- Big expansions get slow-ish - it's distance-transform math, so
expand_pxof ±100 on a large mask takes longer than a naive dilate. You'll rarely need more than ±20, and it's still fast in practice. - Edge quality on tiny masks - with a very small mask the SDF re-threshold can make edges softer than the original. If that matters, expand in two smaller steps.
- Contracting too hard - eroding past about −50 on a thin mask will start eating detail. Go in increments and check the result.
It's a quiet, boring node - and that's the compliment. It does one thing, does it with smooth edges, and it's the right tool for the mask-tidy step that shows up in every serious inpainting workflow.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| expand_px | INT | 0-100–100 | Positive values expand (dilate), negative values contract (erode) |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| processed_mask | MASK | — |