Nodes/ComfyUI-WJNodes/mask line mapping
ComfyUI Node

mask line mapping

A Levels adjustment for masks that stretches contrast automatically

By 807502278·Created 2 years ago·Updated 11 months ago· 20
mask line mapping
  • image
  • mask
  • image
  • mask
smoothtrue
min_target-1
max_target256
min_result0
max_result255
clamp_min0
clamp_max255

Plenty of masks that come out of automated tools - a rembg alpha channel, a soft SAM output, a low-contrast matte from some detection model - never actually hit true black or true white. They sit in a mushy middle range, which makes them weaker than they should be once you use them for compositing or inpainting. If you've ever used the Levels tool in an image editor to stretch a washed-out photo's contrast back out, this node does the exact same thing, applied to a mask (or image) instead.

It's one of the utility nodes in ComfyUI-WJNodes, a personal pack from an author who goes only by 807502278 on GitHub - small, unpolished, genuinely useful for the specific problems it targets.

How it works

You define a source range (min_target to max_target) and a destination range (min_result to max_result). Every pixel value gets linearly remapped from the source range into the destination range - the classic input-black-point / input-white-point / output-black-point / output-white-point idea from photo editing, just expressed as four numbers instead of draggable sliders.

The clever part is the defaults: min_target starts at -1 and max_target starts at 256 - both outside the valid 0–255 pixel range on purpose. When left at those special values, the node automatically detects the actual minimum and maximum values present in your mask and uses those as the source range. In other words, at defaults, this node auto-stretches whatever contrast your mask actually has to fill the full 0–255 range without you having to inspect the mask's histogram yourself first.

clamp_min and clamp_max then clip the final result, so if the remapping would push values outside a range you want to guarantee, this catches it. smooth, on by default, applies smoothing to the remapping.

Inputs and outputs that matter

  • min_target / max_target - the source range to map from. Leave at -1 / 256 for auto-detection; set explicit values if you know exactly what range you're mapping from and don't want it recalculated per-input.
  • min_result / max_result - the destination range, defaulting to the full 0–255 spread.
  • clamp_min / clamp_max - a safety clip on the final output.
  • smooth - boolean, on by default.

Both image and mask are optional inputs, and the corresponding outputs (image, mask) come back remapped the same way - so this works whether you're stretching a mask's contrast or doing the same operation to an actual image.

Where you'd reach for it

The main case is exactly what opened this article: a soft or low-contrast mask from an upstream detector that needs to become a clean, fully-stretched mask before you use it for anything precise. Auto-levels behavior - the -1/256 defaults - handles the common case with zero manual tuning, which is genuinely convenient for a node buried this deep in a utility pack.

Installing it

ComfyUI Manager: search "ComfyUI-WJNodes." Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/807502278/ComfyUI-WJNodes.git

Restart ComfyUI. No models, no heavy dependencies.

Where this gets confusing

The auto-detect defaults are convenient but also mean the node's behavior changes depending on what you feed it - the same settings can produce a mild stretch on one mask and an aggressive one on another, because the source range is recalculated per input rather than fixed. If you need consistent, repeatable remapping across a batch of different masks, set min_target/max_target to explicit values instead of leaving them on auto - otherwise each mask in the batch gets its own independently-detected range, which may not be what you want if you're trying to normalize a whole set the same way.

There's no community discussion anywhere for this specific node - it's part of an obscure personal pack with no public troubleshooting trail - so if a remap looks wrong, the fastest way to understand it is to test on a single known mask with smooth toggled both ways and the target range set explicitly, rather than trusting the auto-detected range blind.

CategoryWJNode/ImageEdit/MaskEdit

Inputs (9)

NameTypeDefaultDescription
smoothBOOLEANtrue
min_targetINT-1-1–256
max_targetINT256-1–256
min_resultINT00–255
max_resultINT2550–255
clamp_minINT00–255
clamp_maxINT2550–255
imageoptIMAGE
maskoptMASK

Outputs (2)

NameTypeDescription
imageIMAGE
maskMASK