Mask Linear Stretch
Drag a washed-out mask back to full contrast
- mask
- MASK
Masks in ComfyUI are supposed to be clean black-and-white, but the ones you get from soft segmenters, depth-based generation, or hand-drawn alpha channels often aren't. They're washed out - gray smears where there should be crisp edges, low contrast between "in" and "out." Mask Linear Stretch fixes exactly that: it remaps the mask's values so the near-dark parts go properly dark and the near-light parts go properly light, and it does it with one of the oldest contrast tricks in image processing.
How it works
It's a percentile-based linear stretch, not a hard threshold. The node takes the mask and finds the pixel values at the low and high percentiles you specify, then stretches everything between them across the full [0, 1] range:
min_val = torch.quantile(mask, tol_low)
max_val = torch.quantile(mask, tol_high)
stretched = (mask - min_val) / (max_val - min_val)
Because it uses quantiles rather than the absolute min/max, a few stray noise pixels don't ruin the stretch - they get ignored. And because it's a stretch, not a threshold, smooth gradients (feathered edges, soft shadows) stay smooth; they just gain contrast. That's the difference between this node and simply binarizing the mask, which is what you'd reach for if you wanted hard edges.
The inputs that matter
mask- the MASK to stretch.tol_low(default0.01) - the low percentile. Values below this get mapped to black.tol_high(default0.99) - the high percentile. Values above this get mapped to white.clip(defaulttrue) - clamp the result to[0, 1]. Keep it on; off is only for when you want values to overshoot the range.
The defaults are sensible: 1% and 99% trim the extremes without discarding real content. If your mask is only slightly washed out, nudge tol_low up and tol_high down for a more aggressive stretch; if it's already decent, widen them.
Output is a single MASK, ready to feed into whatever node was about to receive the mushy version - conditioning, inpainting, compositing, you name it.
Installing it
It's in Duanyll Nodepack. ComfyUI Manager → search "Duanyll Nodepack" → install → restart, or:
cd ComfyUI/custom_nodes
git clone https://github.com/Duanyll/duanyll_nodepack
Find it under duanyll/image. No extra dependencies beyond torch, which you already have.
One note: this is a contrast operation, not a cleanup. If your mask has holes, specks, or jagged edges, stretch them after cleaning, not instead of it. Stretch makes what's there more defined - it won't remove a speck that happens to sit in the 1% you trim.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| tol_low | FLOAT | 0.010–1 | — |
| tol_high | FLOAT | 0.990–1 | — |
| clip | BOOLEAN | true | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| MASK | MASK | — |