Swap Pixels by Mask v5.1.0
Swap pixels between two images, guided by a mask
- from_image
- to_image
- to_mask
- IMAGE
In virtual try-on there's a move you do constantly: you generated the new garment on a floating subject, and now you need to paste those pixels back into the original photo - but only inside the garment region, and only where you want them. Swap Pixels by Mask is the paste node for that job. It copies pixels from one image into another, using a mask to decide where, and it's the closest thing this pack has to a generic compositing utility.
From tri3d-comfyui-nodes (TRI3D-LC, v5.1.0), pure numpy, nothing to download.
How it works
Three images in, one out. from_image (the source of the pixels you're copying) is resized to match the mask, then:
- the mask is inverted internally,
- if
swap_maskedis False (default), pixels are copied into the white region of the mask, - if
swap_maskedis True, pixels are copied into the black region instead.
Everything else in to_image is left bit-identical. That last bullet is the part that trips people: the boolean is named as if True means "swap inside the mask", but True actually swaps the unmasked pixels. Default behavior - the one you'll use 90% of the time - is copy-into-the-white-area.
The typical try-on wiring: to_image is your original photo, from_image is the freshly generated garment shot, and the mask is the garment region you want overwritten. Invert the role of the mask and you can do the reverse - keep the generated garment and restore the original background outside it.
The inputs that matter
from_image- where the copied pixels come from. Resized to the mask's dimensions.to_image- the base image that gets modified in place.to_mask- decides the copy region. Only the first frame of each image is used, so feed single images.swap_masked(BOOLEAN, default False) - False = copy into white regions, True = copy into black regions. Remember the inversion.
Output is one IMAGE: to_image with the selected pixels replaced. No feathering, no alpha blending - it's a hard pixel copy, so you'll usually want to follow it with a blend or inpaint pass to kill the seam.
Install
ComfyUI Manager → search tri3d-comfyui-nodes, or:
cd ComfyUI/custom_nodes
git clone https://github.com/TRI3D-LC/tri3d-comfyui-nodes
Restart ComfyUI. No model downloads.
Gotchas
Beyond the inverted swap_masked naming, the other trap is resolution: from_image gets force-resized to the mask size, so a mismatch between the generated garment and the original photo can look slightly off. And because the copy is hard-edged, don't expect a soft transition - build the feathering upstream with a blurred mask if you need it. Small pack, empty README, but the node itself is dead simple and has no failure modes beyond what you'd expect from a raw paste.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| from_image | IMAGE | — | |
| to_image | IMAGE | — | |
| to_mask | IMAGE | — | |
| swap_masked | BOOLEAN | false | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |