Nodes/comfyui-image-manipulation/Apply Mask To Image (Alpha)
ComfyUI Node

Apply Mask To Image (Alpha)

Bake any mask into real transparency with one two-input node

By Hellfiredragon·Created 2 years ago·Updated 2 years ago· 0
Apply Mask To Image (Alpha)
  • image
  • mask
  • image
  • mask

You've already got a mask - from SAM, BiRefNet, a hand-painted blob, a DensePose body map - and what you actually want is a transparent image. Apply Mask To Image (Alpha) is the bridge: it takes an image and a mask and bakes the mask into the image's alpha channel. Out comes a real RGBA image you can save as a transparent PNG, drop into another scene, or hand to anything that cares about transparency.

That's the whole job, and it's a genuinely useful one. Getting from "I can see the region" to "the region is transparent" is a step ComfyUI leaves to you - this node is the least-friction version of it. It does nothing clever and that's exactly the point.

How it works

Pure PIL, no model involved. The node converts the image and mask tensors back to PIL (tensor2pil / mask2pil), then calls image.putalpha(mask). That single call stamps the mask onto the image as its alpha channel, using the mask's 8-bit values: black (0) becomes fully transparent, white (255) fully opaque.

Which means the quality of your transparency is decided by the quality of your mask, not by this node. Feed it a soft matte from a matting model like BiRefNet and you get soft, hair-friendly edges. Feed it a hard SAM mask and you get a hard cutout - clean, but with the jagged edge look that screams "automated." That hard-versus-soft distinction is the same one that decides most background-removal arguments, and it applies to you right here.

Inputs and outputs

Only two inputs, both required:

  • image (IMAGE) - the picture that gets the transparency.
  • mask (MASK) - what becomes the alpha channel.

Outputs are image (now RGBA) and mask (an exact passthrough of what you fed in). That passthrough is sneaky-useful: you keep the mask alive in the graph for the next step (say, feeding inpaint) without adding a Reroute, and you get the transparent image for compositing. Two birds, one node.

Installing it

It ships in a two-node pack called comfyui-image-manipulation (from Hellfiredragon). Easiest route is ComfyUI Manager - search the pack title and hit install, then restart ComfyUI. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/Hellfiredragon/comfyui-image-manipulation

Then restart ComfyUI. The pack's only declared deps are pillow, numpy, and torch - all things ComfyUI already has. No model downloads, no heavy install, nothing to babysit.

Gotchas

  • The image and mask must be the same resolution. putalpha raises a PIL error if the mask dimensions don't match the image. A mask upscaled from a latent, or generated against a resized reference, will trip this - resize one side first.
  • Not everything downstream keeps your alpha. ComfyUI's graph is fine with RGBA, but plenty of nodes silently drop the alpha channel when they process the image, and the in-graph preview will show the checkerboard (that's normal). Your transparency reliably survives saving to PNG - export there before feeding an RGB-only node.
  • Watch the mode. The mask is converted to 8-bit grayscale (L) on the way in, so a soft mask gets genuine fractional alpha. If that surprises you, it's a feature, not a bug.
CategoryIM Pack

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
maskMASK

Outputs (2)

NameTypeDescription
imageIMAGE
maskMASK