Merge Image Channels (Swwan)
Rebuild an image one channel at a time
- red
- green
- blue
- alpha
- image
What it's for
The complement to Split Image Channels. Split takes an image apart into three (or four) monochrome tensors; Merge puts them back together, and lets each channel come from a different source.
The practical uses are narrower than the name suggests, and worth spelling out. You merge when you're smuggling non-image data through an image-shaped pipe: a depth or normal map that belongs in a specific channel, a mask you want baked into alpha, or a channel-wise operation where you computed new red, green and blue separately and now need one tensor again. KJNodes' original, kept as MergeImageChannels in this pack.
If you're merging three channels that already came from the same image, you don't need this node - you need to not have split it.
How it works, including the fiddly bit
Four inputs. red, green and blue are all required IMAGEs, and alpha is an optional MASK. The implementation is three lines of torch.stack:
- red comes from
red[..., 0, None] - green comes from
green[..., 1, None] - blue comes from
blue[..., 2, None]
Read those indices again - it takes channel 0 from the first input, channel 1 from the second, channel 2 from the third. Not "the red channel concept", the literal channel index of each input. So if you pass a genuinely single-channel grayscale tensor to the green input, you'll index channel 1 of a one-channel tensor and get an error. The intended usage is passing full RGB images whose channels you care about selectively, or the monochrome outputs of a split (which the pack's split node delivers as 3-channel-compatible tensors).
The alpha input is a MASK, not an image, and it gets concatenated as a fourth channel: torch.cat([image, alpha.unsqueeze(-1)], dim=-1). So connecting alpha gives you an RGBA tensor. That's the form the pack's alpha-aware savers want - Save Image with the right alpha mode, RGBA Save, or the Save Image With Alpha legacy node. ComfyUI's stock Save Image will also take it, but if you're juggling transparency you're better off with the pack's RGBA family, whose whole point is that Load Image's mask is 1 - alpha while these nodes carry actual alpha. Mixing the two conventions is the single most common transparency bug in ComfyUI.
Output is one IMAGE, 3-channel without alpha, 4-channel with it.
Install
cd ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan
cd ComfyUI_Swwan
python -m pip install -r requirements.txt
Restart ComfyUI, hard-refresh the tab, search Swwan. Pure torch - no OpenCV, no scipy, no models, and it runs happily on CPU. It lives in Swwan/Advanced/Image next to Split Image Channels, Color Match and Remap Image Range.
Common issues
Index out of range on the green or blue input. You passed a single-channel or mask-shaped tensor. Every one of the three image inputs needs at least three channels.
Batches don't line up. All inputs must have the same batch length, height and width. The stack is positional, so a 4-frame red with a 1-frame green is an error, not a broadcast.
Transparency saved as black. Your alpha convention is backwards. Load Image's mask is inverted alpha; the alpha input here and the pack's RGBA tools are not. Check which end of the pipe is flipped before rewriting the graph.
Downstream node rejects a 4-channel image. Lots of IMAGE inputs assume 3 channels. Drop the alpha connection and save the mask as a separate output instead, or use Images to RGB to fold it back to three.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| red | IMAGE | — | |
| green | IMAGE | — | |
| blue | IMAGE | — | |
| alphaopt | MASK | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |