Nodes/comfy_Pond_Nodes/🐳图像扩展
ComfyUI Node

🐳图像扩展

Align one image to another by matching masks — great for face swaps

By Pondowner857·Created about a year ago·Updated 21 days ago· 45
🐳图像扩展
  • 基准遮罩
  • 遮罩2
  • 图像2
  • 定位后图像
  • 基准遮罩
  • 对齐后遮罩
对齐方式居中对齐
X轴偏移0
Y轴偏移0
填充颜色白色

Face swaps and region replacements have a boring prerequisite problem: the two images aren't framed the same, so nothing lines up. 🐳图像扩展 (ImageAlignByMask) solves the alignment for you - you give it a reference mask (say, a face mask from image A) and a second image with its own mask, and it moves, scales, and repositions image B so its mask lines up with A's. It's called "图像扩展" (image extension) because it also expands the canvas to fit the aligned result. The classic use: line up two faces before an inpainting or swap pass.

The mechanism is straightforward geometry. It computes the bounding box of non-zero pixels in each mask (a get_mask_bounds that finds the min/max coordinates of anything above a tiny threshold), then calculates the transform - translation plus scale - needed to make mask B's bounds match mask A's, according to your chosen alignment. It resizes image B to match, pastes it into a canvas the size of the base image, and fills any leftover area with the 填充颜色 you pick (white, black, or transparent). A X轴偏移/Y轴偏移 pair (±2048px) lets you nudge the result after the math, which you will need, because auto-alignment is never perfect.

The inputs

  • 基准遮罩 - the reference mask everything aligns to.
  • 遮罩2 / 图像2 - the second mask and the image that gets repositioned.
  • 对齐方式 - nine choices: 居中对齐 (center) plus 左/右/上/下 and the four corners. Center is the default and usually right for faces; the corner options matter when one subject is off to the side.
  • X轴偏移 / Y轴偏移 - manual nudges.
  • 填充颜色 - 白色/黑色/透明 for the newly-exposed canvas edges.

Outputs

Three: 定位后图像 (the aligned IMAGE), 基准遮罩 (the reference mask, passed through), and 对齐后遮罩 (image B's mask in its new aligned position). That third output is the useful one - it's the mask you'll feed into the inpainting or compositing that comes next.

Install and the fair warning

From comfy_Pond_Nodes:

cd ComfyUI/custom_nodes
git clone https://github.com/Pondowner857/comfy_Pond_Nodes
cd comfy_Pond_Nodes && pip install -r requirements.txt

Pure torch, no extra deps. Fair warning: alignment quality lives or dies with your masks. If mask B has a different shape than mask A (a wider face, a different pose), the scale-to-fit produces a stretched result - this node matches bounding boxes, not anatomy. For same-pose face swaps it's excellent; for wildly different angles, expect to fight it with the offset knobs. It's a tool, not magic - but it's a tool that removes the manual "eyeball the transform" step from a huge number of workflows.

Category🐳Pond/image

Inputs (7)

NameTypeDefaultDescription
基准遮罩MASK
遮罩2MASK
图像2IMAGE
对齐方式COMBO居中对齐9 options: 居中对齐, 左对齐, 右对齐, 上对齐, 下对齐, 左上对齐, +3
X轴偏移optINT0-2048–2048
Y轴偏移optINT0-2048–2048
填充颜色optCOMBO白色3 options: 白色, 黑色, 透明

Outputs (3)

NameTypeDescription
定位后图像IMAGE
基准遮罩MASK
对齐后遮罩MASK