Nodes/ComfyUI-BrainDead/BD Mask Flatten
ComfyUI Node

BD Mask Flatten

Flatten an image (RGB or RGBA) plus optional alpha mask onto a plain background or another image. Output is always RGB (no alpha) so downstream nodes don't need to handle transparency. Standard alpha composite math: image × alpha + bg × (1 - alpha) — semi-transparent pixels become bg-blended, so removing the bg later via chroma key gives percentage-based recovery. Channel-routing modes pack the image's grayscale into a single RGB channel (useful for game-engine packed maps).

By BizaNator·Created 7 months ago·Updated 16 days ago· 14
BD Mask Flatten
  • image
  • mask
  • background_image
  • image
  • alpha_used
background_colorwhite
background_color_hex#808080
flatten_modealpha_composite
mask_strength1.00
invert_maskfalse
mask_threshold0.00
edge_pad_pixels0
Category🧠BrainDead/Segmentation

Inputs (10)

NameTypeDefaultDescription
imageIMAGE
maskoptMASKOptional alpha mask. OVERRIDES image's alpha channel if both present. If not provided AND image is RGBA, uses image's alpha. If not provided AND image is RGB, treats as fully opaque (alpha=1).
background_imageoptIMAGEOptional image to composite ONTO instead of a solid color. When provided, background_color and background_color_hex are ignored. Resized to match the input image's dimensions.
background_coloroptCOMBOwhiteSolid color background. red/green/blue/magenta = chroma keys. checker = gray checkerboard for debug visualization. custom = use background_color_hex.
background_color_hexoptSTRING#808080Hex color when background_color='custom'. Format: '#RRGGBB' or '#RGB'. Falls back to gray if invalid.
flatten_modeoptCOMBOalpha_compositeHow to flatten: alpha_composite — standard alpha blend image RGB onto background. grayscale — convert image to luminance, alpha-composite onto background. image_to_red — put image's grayscale into ONLY the red channel (green/blue stay as bg). Useful for packing multiple grayscale maps into one RGB image for game engines. image_to_green / image_to_blue — same but for those channels.
mask_strengthoptFLOAT1.000–2Multiply alpha values before composite. >1 makes alpha stronger (less bg shows through), <1 weaker (more bg shows through). Clamped to [0, 1] after.
invert_maskoptBOOLEANfalseInvert the alpha before applying — flattens the OUTSIDE of the mask onto background instead of the inside.
mask_thresholdoptFLOAT0.000–1If > 0, binarize the alpha at this threshold before composite. 0 = soft (continuous alpha), >0 = hard binary cutoff. Useful when you want sharp edges instead of partial transparency on the flatten.
edge_pad_pixelsoptINT00–512Voronoi edge padding — extend foreground RGB outward into transparent areas by N pixels using nearest-foreground color. Standard game-engine 'alpha bleed' to prevent UV-edge halos when textures are sampled at silhouette boundaries. 0 = off (no padding). 16-32 = typical for game textures. Applied BEFORE flattening, so the flatten background only shows beyond the padded zone.

Outputs (2)

NameTypeDescription
imageIMAGE
alpha_usedMASK