🖊️ Fit Item To Mask
The node that drops a cutout product exactly where you want it
- item_image
- item_mask
- canvas_image
- canvas_mask
- item_on_transparent
- item_mask
- item_on_bgcolor
- item_on_canvas
- help_text
This is the node the whole pack is named around, and the one you'll reach for first. You have a product cut out on a transparent background, you have a scene or a model photo, and you want the product composited into it at exactly the size and position you decided on - not wherever a diffusion model decides to put it. yiuFitItm2Msk is that placement step. The author's own framing says it plainly: the mask you draw on the canvas determines where the product goes.
It's the positioning half of an e-commerce pipeline that shows up over and over in this pack: cut the product out (BiRefNet or rembg, see the KB's background-removal doc), place it into the reference photo here, then feed the composite to a Kontext/Flux image generator to blend the whole scene. The example workflow that ships with the repo does exactly this, right down to an auto-downloading BiRefNet cutout node feeding it.
How it works
The mechanism is a small, honest geometry problem. The node takes the bounding box of the mask you drew on the canvas image, then scales your item to fit inside that box and centers it there - classic CSS object-fit behavior:
- fit scales the item so it's fully inside the mask box, letterboxed.
- fill scales it so it covers the whole box, cropping the overflow.
The item is resized with Lanczos and pasted at the computed position. Nothing generative happens; this is pixel placement, which is exactly why it's predictable.
The inputs that matter
- item_image / item_mask - your cutout and its mask. Feed it the same mask your cutout node produced, or the alignment will be off.
- canvas_image / canvas_mask - the scene, and the mask that says where the product should sit. This mask is the whole control surface.
- object_fit -
fitvsfill(defaultfit). - bg_color - a
#RRGGBBor 3-digit hex for the preview composite, default black.
Outputs
Four images come out: item_on_transparent (the placed item with a transparent background - the one to wire into your generator or compositor), item_mask (the aligned mask, handy for feeding inpaint or a subsequent yiuImgCnvPlacer), and item_on_bgcolor / item_on_canvas, which are just preview composites on the solid color or the original background.
Installing it
This ships in yiu_comfy_nodes by leizingyiu. Easiest: ComfyUI Manager → Install Custom Nodes → search yiu_comfy_nodes. Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/leizingyiu/yiu_comfy_nodes
Restart ComfyUI after. Dependencies are just torch/numpy/Pillow - nothing to download, no model files (the cutout model comes from whatever background-removal pack you already use).
Where people get burned
- Empty canvas mask. If your canvas_mask has no drawn pixels, the node raises "mask contains no valid region." Draw something in it - a blob is fine, the bbox is what matters.
- bg_color that doesn't start with
#throws an error on purpose. It accepts 3-digit hex here (unlike the sibling yiuImgCnvPlacer, which demands six digits - annoying inconsistency, worth knowing). - Mismatched item mask. If you feed a cutout image from one node and a mask from another, the fit math works off the image size while the alpha comes off the mask, and you get ghosting. Keep them from the same cutout node.
One more thing worth knowing: every node in this pack appends a help_text string output with the author's own usage notes, and they're localized - delete the cn... file in yiu_comfy_nodes/yiu_comfy_nodes_impl/lang_setting to force English UI text if your system locale is Chinese.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| item_image | IMAGE | — | |
| item_mask | MASK | — | |
| canvas_image | IMAGE | — | |
| canvas_mask | MASK | — | |
| object_fit | COMBO | fit | 2 options: fill, fit |
| bg_color | STRING | #000000 | — |
Outputs (5)
| Name | Type | Description |
|---|---|---|
| item_on_transparent | IMAGE | — |
| item_mask | MASK | — |
| item_on_bgcolor | IMAGE | — |
| item_on_canvas | IMAGE | — |
| help_text | STRING | — |