Nodes/ComfyUI-QING/图像遮罩丨转换
ComfyUI Node

图像遮罩丨转换

Image ↔ mask conversion, two channels at a time

By sheengoa·Created about a year ago·Updated 20 days ago· 16
图像遮罩丨转换
  • image1
  • image2
  • mask1
  • mask2
  • 遮罩1
  • 遮罩2
  • 图像1
  • 图像2
channelred
mask_output_dim3D (B,H,W)

ComfyUI core gives you Image To Mask and Mask To Image as separate nodes. This pack merges the whole round-trip into one node - and gives you two conversion channels at once, which is the trick that saves you from building parallel conversion chains. ImageMaskConverter converts an image into a mask (by pulling out one color channel) and a mask into a grayscale image, for up to two inputs each, in a single step.

It's from ComfyUI-QING (display name "图像遮罩丨转换"), and it's one of those nodes you'll reach for when you're juggling alpha channels and segmentation masks in the same graph.

How it works

The mechanism is dead simple under the hood: image → mask grabs one channel of the image (red, green, blue, or alpha) and drops it into a MASK tensor. Pick alpha on an image that has no alpha channel and it falls back to a luminance (grayscale) conversion instead of erroring - a nice touch. Mask → image stacks the mask across R, G, B, which produces a grayscale-looking IMAGE you can preview or feed to nodes that only take images.

The inputs:

  • channel - required, which channel to extract for image→mask conversion. Default red.
  • mask_output_dim - optional, 3D (B,H,W) (default) or 2D (H,W). The 2D option drops the batch dimension, handy if a downstream node is picky about shape.
  • image1, image2 - up to two IMAGE inputs for image→mask.
  • mask1, mask2 - up to two MASK inputs for mask→image.

The outputs mirror it: 遮罩1, 遮罩2 (MASK) and 图像1, 图像2 (IMAGE). You can feed both directions at once - e.g. an image and a mask in, and the converted versions of both out - which is the "two channels at a time" feature in action.

When you'd actually use this

The classic ComfyUI motion is: background-removal node spits out a mask → you want to see that mask as an image to verify it → Mask To Image + preview. This node does that. But the channel-extraction half is where it earns its keep: alpha-to-mask is the single most useful conversion in this pack. Got a PNG with real transparency? Extract the alpha channel into a mask and you've got a perfect selection region for inpainting or compositing without any segmentation model. And because it's channel-based, you can also pull just the red or green channel if a mask is encoded in color.

There's a deliberate quirk to know: empty inputs come back as empty 64×64 placeholders, not as "nothing." If you only wire mask1, the node still outputs a black 64×64 图像1 in the image slot. That's so the node always returns the same output structure, but it means "I didn't convert that side" is invisible - you just get a blank square. Don't wire things downstream that you didn't actually feed.

Installing

It ships in ComfyUI-QING, so install the pack once and every node appears. ComfyUI Manager: search "ComfyUI-QING". Or:

cd ComfyUI/custom_nodes
git clone https://github.com/GAO-SHIQING/ComfyUI-QING
cd ComfyUI-QING
python install_dependencies.py   # or: pip install -r requirements.txt

Restart ComfyUI. Plain pip dependencies (Pillow, opencv-python, scipy, scikit-image, cairosvg), no model downloads. China users: python install_dependencies.py --mirror --auto. Python ≥ 3.9, current ComfyUI.

Gotchas

Beyond the placeholder behavior: when you pick alpha and there's genuinely no alpha channel, it converts to luminance - which is useful but means the "alpha" you asked for is actually brightness, so don't be surprised by the result. And the 2D mask_output_dim option takes only the first frame of the batch (mask[0]), so if you need per-frame 2D masks from a batch you'll want to stay in 3D mode and squeeze elsewhere. Small print, but it bites in batch workflows.

CategoryQING/图像处理

Inputs (6)

NameTypeDefaultDescription
channelCOMBOred4 options: red, green, blue, alpha
mask_output_dimoptCOMBO3D (B,H,W)2 options: 3D (B,H,W), 2D (H,W)
image1optIMAGE
image2optIMAGE
mask1optMASK
mask2optMASK

Outputs (4)

NameTypeDescription
遮罩1MASK
遮罩2MASK
图像1IMAGE
图像2IMAGE