π§ Mask Layout Fit
Resize and position a subject to sit neatly inside a frame
- image
- mask
- image
- mask
This node takes an image with a masked subject and repositions it so the subject sits neatly inside a target frame, with margins you control. Think of it as auto-layout for a cutout: you've got a person, a product, an object isolated by a mask, and you want it centered and scaled to fill a canvas with consistent padding on every side. Instead of eyeballing scale and position by hand, you give it the margins and a max output size, and it does the geometry.
It's a compositing and layout helper, and it shines for anything template-shaped - thumbnails, product cards, catalog images, consistent framing across a batch. The node's own description is precise about what it does: it resizes and moves the image by the mask's bounding box so the mask lands cleanly within the layout minus the margins.
How it works
It reads the mask's bounding box - the tight rectangle around the masked pixels - and treats that as the subject. Then it computes the scale and offset needed to fit that box inside the output frame, leaving the margins you specified as empty border on each side, and applies the same transform to both the image and its mask so they stay aligned. When the repositioning exposes area that had no image (because the subject got moved or scaled), fill_mode decides what goes there.
The inputs that matter
- image and mask - the picture and the mask that marks the subject. The mask's bounding box is what drives the fit.
- margin_top / margin_bottom / margin_left / margin_right - the padding, in pixels, to leave on each side. Set them equal for centered, even framing; set them uneven to bias the subject toward a corner or edge.
- max_output_side - caps the longest side of the output (default 1024). This sets the resolution ceiling of the framed result.
- fill_mode - what fills newly-exposed areas:
white(a clean white border, great for product/catalog looks) oredge_nearest(stretch the nearest edge pixels, which blends more naturally into a photographic background).
Outputs are image and mask - the repositioned image and its correspondingly moved mask, both ready for compositing or a downstream inpaint/generation pass.
Common issues
The subject is off-center or cropped. Check your margins - they eat into the available frame, so large margins shrink the space the subject fits into. If it's getting clipped, your mask bounding box may be larger than you think (a stray masked pixel in a corner blows the box out). Clean the mask so it tightly wraps only the subject.
Ugly border where the image got moved. That's fill_mode. white gives a hard white fill (intentional for catalog shots); switch to edge_nearest if you want the exposed area to blend rather than show a flat block.
Output looks low-res. max_output_side caps the size. Raise it if you need more pixels, within reason for your VRAM.
The mask and image don't line up after. They should - the node transforms both together. If they've drifted, confirm you're feeding the mask that actually corresponds to the image, not one from a different source.
Installing SDVN_Comfy_node
ComfyUI Manager: search "SDVN_Comfy_node" β install β restart. Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/StableDiffusionVN/SDVN_Comfy_node
Then pip install -r custom_nodes/SDVN_Comfy_node/requirements.txt from the ComfyUI root and restart. The node is under π SDVN / ποΈ Image.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | β | |
| mask | MASK | β | |
| margin_top | INT | 00β16384 | β |
| margin_bottom | INT | 00β16384 | β |
| margin_left | INT | 00β16384 | β |
| margin_right | INT | 00β16384 | β |
| max_output_side | INT | 10241β16384 | β |
| fill_mode | COMBO | white | 2 options: white, edge_nearest |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | β |
| mask | MASK | β |