Apply Mask To Image
Turn a mask into real transparency without the save-PNG-reload dance
- image
- mask
- masked
Apply Mask To Image does one clean thing: it takes an image and a mask, and merges them into a single RGBA image where the mask becomes the alpha channel. Black in the mask means transparent, white means opaque - the README's way of putting it is "black mask areas become transparent," and that's exactly right.
You reach for it whenever you have a mask and you want transparency in the graph, not after a round-trip through a file. Say a background-removal model gives you a subject mask, or a segmentation node hands you a clean cutout mask. You want to composite that subject onto another scene, or save it as a transparent PNG. That's two steps: this node to stamp the mask into the alpha channel, then Save Image (which writes RGBA fine) - no intermediate file, no quality loss, no forgetting which mask goes with which image.
How it works
The source is unusually defensive, in a good way. It accepts 3- or 4-channel images in the standard [B, H, W, C] layout, and it's tolerant of mask layouts - plain [B, H, W], or squeezed [B, 1, H, W] / [B, H, W, 1] - normalizing them all. A single mask is expanded across a whole batch of images. And there's a sensible rule for already-transparent input: if your image already has an alpha channel, the mask is multiplied into it, so mask-black still zeroes out the alpha.
Two real constraints, both from the code: the mask must match the image exactly in size, and batch counts must match (or be 1). Mismatch and it throws a clear error telling you what it expected. So if your mask came off a differently-sized crop, resize it first - this pack's own ResizeToMultiple or a standard resize node handles that.
Gotchas worth knowing
The output is always 4-channel RGBA. That's what you want for saving, but be aware some preview widgets composite alpha over a checkerboard - a "transparent" preview doesn't mean your pixels are gone. They aren't. This node only sets alpha; the RGB data underneath is untouched. If you composite the result onto something, the old pixels still exist under the transparent area and can show through at the edges if you're not careful - that's why the usual pipeline masks out the background (or uses a fill) before compositing.
Don't confuse this with background removal. This doesn't cut anything out; it just labels what's transparent. The cutout behavior is the sum of this node plus whatever produced the mask.
Install
Part of the tiny ComfyUI-Image-Resizing pack - no models, no downloads, dependencies are the standard torch/Pillow/numpy/OpenCV stack. Install via ComfyUI Manager (search "ComfyUI-Image-Resizing") or:
cd ComfyUI/custom_nodes
git clone https://github.com/Zar4X/ComfyUI-Image-Resizing
Restart ComfyUI and it'll be under Image Resizing/Mask. It's a small node with a very specific job - but that job (mask to real transparency, in-graph, instantly) is one you'll want on tap.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| output_mode | COMBO | transparent | 3 options: transparent, black, white |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| masked | IMAGE | — |