π Gemini Mask Resize
Grow or shrink a mask without wrecking its shape
- mask
- mask
Gemini Mask Resize is a two-knob local utility: expand or shrink a mask, then optionally soften its edge. That's the whole node, and it's useful precisely because mask math this simple is everywhere and fiddly to do right by hand.
You end up needing it constantly in the detailing loop from the KB. A segmentation mask hugs a subject, but your inpaint or refine pass wants a slightly bigger region so the seam lands outside the actual edge - that's expand_pixels positive. Or the mask bleeds into the background and you want to pull it back - that's negative. Then a little feathering so the composited result doesn't have a hard edge.
How it works
Two controls:
- expand_pixels (default 0, range β2048β¦+2048) - grow (+) or shrink (β) the mask by N pixels. The implementation detail matters here: it uses a Euclidean distance transform, so the expansion is round - it follows the silhouette rather than just translating a kernel outward, and it runs in constant time no matter how big N is. Positive values fill in concavities naturally; negative values erode from the boundary. This is the knob for "give me the mask, but bigger."
- feather_pixels (default 0, range 0β¦512) - a soft edge applied after the expand/shrink, via a separable Gaussian (1D convolutions in both axes). That's the "cheap blur" version of a 2D blur, and it's what makes the seam disappear when you composite.
The input is a MASK batch, and each mask in it is processed independently around its own silhouette - so a batch of different-sized blobs each grows by the same pixel count, which is what you want when every blob's seam margin should be uniform in pixels rather than uniform in fraction.
Outputs and wiring
One output: mask, a MASK batch of the same shape as the input. It's a pure transform node - nothing else comes out, no metadata, no preview. In practice it slots between a mask producer (Gemini Segmentation, Gemini Separate Mask Components) and anything that consumes a mask: a compositor like Gemini Mask Stitch, an inpaint conditioning node, or a crop node. It pairs especially well with Mask Stitch's pre_shrink_alpha philosophy - you often want the composite alpha shrunk to kill a halo, and here you can do the same to the mask itself before it ever reaches a stitch.
Install
Pack-wide install, as always: ComfyUI Manager search "ComfyUI_Gemini", or clone into custom_nodes and pip install -r requirements.txt. This node needs nothing extra - it's pure torch, no scipy, no API key, no network. Restart ComfyUI and it appears under AI/Image Analysis/Google.
There isn't much to break, but two habits help. First, if your mask looks chunky after feathering, remember the order is fixed: expand first, feather second - you can't feather then expand, and the node doesn't expose that ordering. Second, extremely negative expand_pixels on a small mask can erase it to nothing; the distance transform just returns an empty mask rather than erroring, so check your preview if a region vanished. That's the entire troubleshooting list - it's a small, honest node, and that's a feature.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | Mask batch (B, H, W). Each mask is grown/shrunk independently around its own silhouette. | |
| expand_pixelsopt | INT | 0-2048β2048 | Grow (+) or shrink (-) the mask by N pixels uniformly. Uses Euclidean distance β the expansion is round, follows the silhouette, and runs in constant time regardless of N. |
| feather_pixelsopt | INT | 00β512 | Soft edge applied AFTER expand/shrink. Separable Gaussian via 1D conv β much faster than 2D blur. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| mask | MASK | β |