Nodes/ComfyUI-Apt_Preset/create_Mask_match_shape2
ComfyUI Node

create_Mask_match_shape2

The current version of Apt_Preset's mask-fit compositor

By cardenluo·Created 2 years ago·Updated 18 days ago· 309
create_Mask_match_shape2
  • bimg
  • bmask
  • fimg
  • fmask
  • composed_image
  • new_background
  • foreground_layer
  • foreground_mask
b_colorblue
b_extrant_to_blocktrue
f_extrant_to_blocktrue
f_smoothness1
f_opacity1.00
bjimg_colorblue
f_x_offset0
f_y_offset0
f_scale1.00
scale_modeauto-in
align_modecenter
f_rot0
image_outputPreview

This is the actively-maintained successor to create_Mask_match_shape - same core job, composite a masked foreground onto a masked background while fitting it to the target region, but with clearer, documented semantics for exactly how the fitting works. If you're starting fresh and both nodes show up in your search, this is the one to use.

What it is and why you'd reach for it

Compositing a foreground onto a background where you don't want to eyeball the scale and position by hand is a common enough need - sticker-style cutout placement, swapping an object into a scene at the right size, building composite reference images. This node handles the "does the foreground actually fit the target region" problem for you, with two clearly-defined auto-fit modes instead of guesswork.

How it works - straight from the node's own docstring

Unusually, this node ships its own explanation in its description field, which is about as authoritative as it gets:

scale_mode - options are None, width_align, height_align, auto-in, auto-out. None disables auto-scaling. auto-in means the foreground's valid (unmasked) area ends up fully contained within the background's valid area. auto-out means the background's valid area ends up fully contained within the foreground's valid area. align_mode is a 9-position grid alignment choice.

That's a real, meaningful distinction: auto-in shrinks the foreground to fit inside the target region (nothing spills over the edges), auto-out grows it to cover the region completely (the foreground may extend past it). Pick based on whether you want the foreground fully contained or the background fully covered.

One thing the schema doesn't fully spell out: there are two separate color pickers here, b_color (7 options, including using bimg itself as the fill) and bjimg_color (6 options) - both clearly background-fill related, but the exact division of labor between them isn't documented. The fastest way to learn it is to toggle one, leave the other fixed, and watch how new_background changes.

Inputs and outputs that matter

  • bimg / bmask (required) - background image and mask.
  • scale_mode (optional, default auto-in) - the fit behavior described above.
  • align_mode (optional, default center, 9 positions) - where the foreground sits within the target region: corners, edge-centers, or dead center.
  • f_smoothness / f_opacity - edge feathering and blend strength on the foreground.
  • fimg / fmask (optional) - the foreground; f_x_offset/f_y_offset, f_scale, f_rot for manual fine-tuning on top of the auto-fit.

Outputs: composed_image, new_background, foreground_layer, foreground_mask - the full composite plus its individual layers.

Installing it

Through ComfyUI Manager: search ComfyUI-Apt_Preset, install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/cardenluo/ComfyUI-Apt_Preset.git

Run install.bat on Windows for dependencies; on Linux/Mac install the requirements yourself. No models needed - pure compositing math.

Common issues

If your foreground looks clipped when you expected it fully visible, you're probably on auto-out when you wanted auto-in (or vice versa) - re-read the two definitions above, they're easy to swap in your head since both start with "auto."

If the background color isn't changing the way you expect, remember there are two separate pickers (b_color and bjimg_color) - you may be adjusting the one that isn't driving the effect you're looking at. As with the rest of ComfyUI-Apt_Preset: it's a large, actively-updated pack, and at least one user has reported an IMPORT FAILED error at ComfyUI startup after installing it. Check the console log if this node doesn't appear after install.

CategoryApt_Preset/mask

Inputs (17)

NameTypeDefaultDescription
bimgIMAGE
bmaskMASK
b_colorCOMBOblue7 options: bimg, black, white, red, green, blue, +1
b_extrant_to_blockBOOLEANtrue
f_extrant_to_blockBOOLEANtrue
f_smoothnessINT10–150
f_opacityFLOAT1.000–1
bjimg_colorCOMBOblue6 options: black, white, red, green, blue, gray
fimgoptIMAGE
fmaskoptMASK
f_x_offsetoptINT0-500–500
f_y_offsetoptINT0-500–500
f_scaleoptFLOAT1.000.01–5
scale_modeoptCOMBOauto-in5 options: None, width_align, height_align, auto-in, auto-out
align_modeoptCOMBOcenter9 options: center, top_center, bottom_center, left_center, right_center, top_left, +3
f_rotoptINT0-180–180
image_outputoptCOMBOPreview4 options: Hide, Preview, Save, Hide/Save

Outputs (4)

NameTypeDescription
composed_imageIMAGE
new_backgroundIMAGE
foreground_layerIMAGE
foreground_maskMASK