ComfyUI Node

PIL To Mask

Converts a Pillow image back — but the output is still typed IMAGE

By Chaoses-Ib·Created 3 years ago·Updated 8 months ago· 56
PIL To Mask
  • images
  • IMAGE

PILToMask looks like it should be the mask-flavored twin of PILToImage - same PIL_IMAGE input, obviously meant for mask data - and functionally that's exactly the job: it converts a PIL.Image you built or edited in Python back into something ComfyUI can use as a mask. The one thing worth knowing before you wire it up: per the node's own definition, its output socket is typed IMAGE, not MASK. Drag a wire from it straight into a node that demands a genuine MASK type - an inpainting sampler's mask input, VAE Encode (for Inpainting), a Set Latent Noise Mask - and it may not accept it as-is. ComfyUI's type system checks the socket type, not the node's name, and this one's socket says image.

That's not a bug you need to work around by avoiding the node - it's just an extra step. Run the output through ComfyUI's stock Convert Image to Mask node afterward, and you've got a real MASK-typed wire to feed anywhere ComfyUI expects one.

Where this is actually useful: masks in ComfyUI are single-channel data under the hood, and building one by hand in Python - drawing a rectangle or a feathered shape with Pillow's ImageDraw, thresholding something, compositing a few shapes together - is often easier with Pillow's own drawing API than with tensor math. That's squarely a ComfyScript real-mode use case (the same author's Python front-end for ComfyUI): script the mask construction in Pillow, then bring it back into the graph with this node. It's the same round-trip idea as PILToImage, just for content you intend to use as a mask rather than a picture.

The inputs and outputs that matter

  • images (PIL_IMAGE) - the only input, a Pillow image object (this pack's ImageToPIL, or another PIL-producing node, feeds it).
  • Output: IMAGE - note the type, not MASK. If your next node insists on MASK, add a Convert Image to Mask node in between.

How to install it

Same install as the rest of the pack. Via ComfyUI Manager: search "ComfyUI_Ib_CustomNodes". Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/Chaoses-Ib/ComfyUI_Ib_CustomNodes

then restart. No models, no extra dependencies - it's a thin wrapper around Pillow, which ships with ComfyUI already.

Common issues & troubleshooting

"MASK input" won't accept the wire. This is the one everyone hits first, which is why this article leads with it: the output here is IMAGE-typed. Route it through Convert Image to Mask before it reaches anything that specifically demands MASK.

The mask looks inverted or the wrong channel got used. Convert Image to Mask lets you pick which channel to read - red, green, blue, or alpha. If your Pillow-side mask was built as plain grayscale (L mode), the channel picker matters less; if you handed it an RGBA or RGB image, double-check which channel actually carries your mask data before you go debugging the wrong thing downstream.

You don't actually need PIL for this. If all you want is a simple hand-painted or thresholded mask and you're not scripting anything in Python, ComfyUI's own mask editor (right-click a loaded image → Open in MaskEditor) or a plain Convert Image to Mask on a black-and-white image gets you there without touching PIL at all. This node earns its place specifically when the mask itself is easier to construct in code.

Categoryimage/PIL

Inputs (1)

NameTypeDefaultDescription
imagesPIL_IMAGE

Outputs (1)

NameTypeDescription
IMAGEIMAGE