Nodes/ComfyUI-Portrait-Maker/MaskMerge2Image PM
ComfyUI Node

MaskMerge2Image PM

Where the Mask Says 'Show Me This' — Compositing Two Images With a MASK

By THtianhao·Created 3 years ago·Updated 2 years ago· 201
MaskMerge2Image PM
  • image1
  • image2
  • mask
  • IMAGE

PM_MaskMerge2Image is the node that actually puts the portrait back into the photo. EasyPhoto-style workflows split into two halves - generate or enhance a face in isolation, then paste it back - and this is the paste step. You hand it image one, image two, and a mask, and it shows image one where the mask is white and image two where the mask is black.

How it works

The math is one line, and it's exactly what it sounds like: image1 * mask + image2 * (1 - mask). Every pixel where the mask is bright keeps image1; where the mask is dark keeps image2. The mask is treated as a per-pixel weight, which is the real feature here - because it's a weighted blend rather than a hard cut, a soft (blurred) mask gives you a soft, feathered transition between the two images. A hard-edged mask gives you a hard-edged seam. Same node, and the quality of your result is entirely decided by the mask you feed it.

This is how the pack's face pipeline closes the loop: PM_PortraitEnhancement (or makeup transfer, or a face swap) produces a touched-up face image, the face mask from PM_RetinaFace/PM_FaceSkin says where that face lives, and MaskMerge2Image composites the improved face back onto the untouched original background. Everything else in the workflow is just making that one blend not look like a blend.

The inputs

Three inputs, one output:

  • image1 - shown where the mask is white.
  • image2 - shown where the mask is black.
  • mask - the per-pixel weight deciding which shows.

Output is a single IMAGE. Note there's no "blend amount" or feathering control - this is a straight composite. If you want a soft edge, you blur the mask upstream (ComfyUI's built-in Blur Mask works, or chain this pack's PM_MaskDilateErode to grab the edge band, blur it, and add it to your mask for a controlled transition).

The gotchas that actually bite

Dimension mismatch. All three inputs should be the same size. image1 * mask + image2 * (1 - mask) relies on tensor broadcasting, and while ComfyUI will often quietly broadcast, feeding a 1024×1024 image1 with a 512×512 image2 is how you get either an error or an output you didn't intend. When in doubt, normalize sizes first with PM_ImageScaleShort or PM_ImageResizeTarget.

Black/white semantics. Double-check which image you think is "on top." The mask shows image1 on white, image2 on black - flip it and your face region suddenly shows the background. Classic beginner stumble, and the fix is just inverting the mask, but it's worth checking the first time.

Hard seams. If your mask came from a matting model, it probably has a crisp boundary. Compositing with a hard mask at full resolution shows every pixel of that boundary. Blur the mask - even a small Gaussian blur - before merging. It's the difference between "professional composite" and "I can see the cutout."

Install

This node ships in ComfyUI-Portrait-Maker. ComfyUI Manager (search "Portrait-Maker") or:

cd ComfyUI/custom_nodes
git clone https://github.com/THtianhao/ComfyUI-Portrait-Maker.git

then restart ComfyUI. First launch is the rough part: the pack installs heavy deps (tensorflow, insightface, modelscope, pinned diffusers 0.18.2) and downloads a pile of models with no hash verification - interrupted downloads need manual deletion and a re-download. MaskMerge2Image itself needs none of that (it's tensor arithmetic), but you take the whole pack or nothing. And yes, it lives under the protrait/ category - the pack spells it wrong. It's not you.

This is the quiet workhorse of the pack. Not glamorous, but every time a face looks like it genuinely belongs in the photo, this node is usually why.

Categoryprotrait/model

Inputs (3)

NameTypeDefaultDescription
image1IMAGE
image2IMAGE
maskMASK

Outputs (1)

NameTypeDescription
IMAGEIMAGE