Nodes/BrainDead Nodes/BD Luminance Mask
ComfyUI Node

BD Luminance Mask

Pull the brightest and darkest pixels out as masks — percentiles, not magic numbers

By BizaNator·Created 8 months ago·Updated 3 days ago· 15
BD Luminance Mask
  • image
  • mask
  • highlight_mask
  • dark_mask
  • threshold_hi
  • threshold_lo
luma_standardbt709
top_percent10.00
bottom_percent8.00
exclude_above1.00
exclude_below0.00
top_soft_edge0.030
bottom_soft_edge0.030
smooth_pixels0
gradient_modefalse
gradient_curve1.00

BD Luminance Mask finds the brightest and darkest parts of an image and hands them to you as masks. It sounds like a one-line filter, but the detail that makes it actually usable is that it doesn't use fixed brightness thresholds - it uses percentiles. Top 10% brightest and bottom 8% darkest are computed from this image's own histogram, so a dark, moody render still produces a meaningful highlight mask from its brightest regions, and a blown-out image still yields a useful dark mask. A fixed threshold at 0.9 would give you all-white or all-black garbage on half your inputs; this adapts.

The use case in this pack is game-asset prep. Shading zones - where a shader should treat pixels as highlight, midtone, or shadow - are classic mask inputs, and so is pulling specular-ish brights off a character render before you pack channels. The pack's own Mask Tools workflow template wires it with BD Mask Flatten, crop-to-mask, and fill-holes, so it's meant to sit upstream of flattening: you extract the highlight mask, decide what to do with it, and flatten the result onto a clean background.

The inputs that matter

There are a lot of knobs here, but only a few you'll actually touch:

  • top_percent / bottom_percent - how much of the histogram to keep on each end. 0 disables that output entirely.
  • mask - restrict the percentile calculation to a region (e.g. your skin mask). This is the quiet hero input: pass the skin mask and clothing/background can't skew your thresholds. The output masks are also clipped to this region.
  • exclude_above / exclude_below - the escape hatch for degenerate images. If your render has lots of pure-white reflections or a pure-black background, the percentile collapses to 1.0 or 0.0 and your mask is meaningless. Excluding everything above 0.95 (or below 0.05) clips those out of both the calculation and the output.
  • gradient_mode - flip this on and the binary mask becomes a smooth ramp: brightest pixel = white, threshold pixel = black, smooth gradient between. That's the "fade from white to black" look, driven by gradient_curve (1.0 linear, <1 ramps fast to white, >1 keeps most pixels dark).

The two soft_edge fields feather the binary threshold (pixels just below the cutoff ramp 0→1), and smooth_pixels is a spatial Gaussian blur that spreads the mask region but doesn't change value relationships. smooth_pixels blurs in space; gradient_mode fades in luma - different tools, don't mix them up when troubleshooting.

Outputs are highlight_mask, dark_mask, and the two computed thresholds (threshold_hi, threshold_lo) so you can see what cutoff the percentile actually picked.

A quick gotcha

The classic failure: an image with a huge pure-white background makes highlight_mask select the background, not the subject's shiny bits. That's what exclude_above exists for - set it to ~0.95 first, then set top_percent smaller than the background fraction. And if you're doing stylized character work, wire the mask input from the start; it's the difference between masks that track your subject and masks that track the scenery.

Installing

Same as the whole pack: ComfyUI Manager → search "BrainDead" → install, or git clone https://github.com/BizaNator/ComfyUI-BrainDead into custom_nodes, pip install -r requirements.txt, restart. Keep ComfyUI itself current - all BrainDead nodes are V3 API.

It's one of those unglamorous nodes you don't think about until the moment you need a "skin highlights only" mask and realize there's no built-in way to get one. Now there is.

Category🧠BrainDead/Segmentation

Inputs (12)

NameTypeDefaultDescription
imageIMAGESource image. Luminance computed via the selected weighting.
luma_standardCOMBObt709RGB → luminance weighting: bt709 (default): 0.2126 R + 0.7152 G + 0.0722 B — modern sRGB/Rec.709 standard. Use for Unity/Unreal, AI pipelines, modern display work. bt601: 0.299 R + 0.587 G + 0.114 B — legacy NTSC weights, matches Photoshop 'Desaturate' and older tools. average: (R+G+B)/3 — simple, not perceptual.
top_percentFLOAT10.000–50Extract pixels in the top X% of luminance. Set to 0 to disable highlight output. If your image has lots of pure-white pixels (reflections, background), the percentile will collapse to 1.0 — use exclude_above to clip those out first.
bottom_percentFLOAT8.000–50Extract pixels in the bottom Y% of luminance. Set to 0 to disable dark output. If your image has lots of pure-black pixels (background, lines), use exclude_below.
exclude_aboveFLOAT1.000–1Pixels with luminance ABOVE this are excluded from BOTH the percentile calculation AND the output mask. Use to ignore pure-white reflections/background (e.g. 0.95 excludes pixels brighter than 95%). Default 1.0 = no exclusion.
exclude_belowFLOAT0.000–1Pixels with luminance BELOW this are excluded from BOTH the percentile calculation AND the output mask. Use to ignore pure-black background/lines (e.g. 0.05 excludes pixels darker than 5%). Default 0.0 = no exclusion.
top_soft_edgeFLOAT0.0300–0.3Soft edge BELOW the highlight threshold — pixels at or above thr_hi are fully selected (=1), pixels in [thr_hi - top_soft_edge, thr_hi] ramp 0→1. 0 = hard binary edge. Bigger = wider luma-range feather (smooths threshold).
bottom_soft_edgeFLOAT0.0300–0.3Soft edge ABOVE the dark threshold — pixels at or below thr_lo are fully selected (=1), pixels in [thr_lo, thr_lo + bottom_soft_edge] ramp 1→0. 0 = hard binary edge. Bigger = wider luma-range feather.
smooth_pixelsFLOAT00–200SPATIAL gaussian blur radius in pixels applied to BOTH output masks. This spreads the mask spatially (softens chunk edges) but doesn't change the value relationships. Typical: 4-16 subtle, 32+ heavy. 0 = no blur.
gradient_modeBOOLEANfalseWhen ON: mask values become a SMOOTH RAMP based on how far each pixel's luminance is from the threshold toward the extreme. Brightest pixel = white, threshold pixel = black, smooth gradient between (no binary cutoff). Ignores soft_edge. This is the 'fade from white to black' look — different from smooth_pixels (which is spatial blur).
gradient_curveFLOAT1.000.2–4Shape of the gradient ramp (only when gradient_mode is ON). 1.0 = linear. <1 (e.g. 0.5 = sqrt) ramps quickly toward white. >1 (e.g. 2.0) keeps most pixels dark, only brightest reach white.
maskoptMASKOptional region mask. When provided, percentile calculation only considers pixels where this mask > 0.5 (typically the skin mask). Output masks are also restricted to this region (zero outside).

Outputs (4)

NameTypeDescription
highlight_maskIMAGEWhite (1) where pixels are in the top X% of luminance, black (0) elsewhere.
dark_maskIMAGEWhite (1) where pixels are in the bottom Y% of luminance, black (0) elsewhere.
threshold_hiFLOATThe computed luminance threshold above which pixels become highlight_mask=1.
threshold_loFLOATThe computed luminance threshold below which pixels become dark_mask=1.