Nodes/Duanyll Nodepack/Mask Linear Stretch
ComfyUI Node

Mask Linear Stretch

Drag a washed-out mask back to full contrast

By Duanyll·Created about a year ago·Updated 4 months ago· 2
Mask Linear Stretch
  • mask
  • MASK
tol_low0.01
tol_high0.99
cliptrue

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 (default 0.01) - the low percentile. Values below this get mapped to black.
  • tol_high (default 0.99) - the high percentile. Values above this get mapped to white.
  • clip (default true) - 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.

Categoryduanyll/image

Inputs (4)

NameTypeDefaultDescription
maskMASK
tol_lowFLOAT0.010–1
tol_highFLOAT0.990–1
clipBOOLEANtrue

Outputs (1)

NameTypeDescription
MASKMASK