D2 Cut By Mask
Cut an object out of an image with a mask, cleanly
- images
- mask
- image
- mask
- rect
Sometimes you want the thing, not the picture of the thing. D2 Cut By Mask takes an image and a mask and carves out just the masked region - as a loose mask-shaped cutout, a tight rectangle, or a square thumbnail centered on the subject. It's the extraction half of a cut-and-paste pair with D2 Paste By Mask, and it's genuinely useful for object isolation, subject crops for training data, and building clean inpainting inputs.
How it works
You give it an IMAGE and a MASK, and it finds the nonzero (masked) region and cuts around it. The cut_type decides the shape of the cut:
mask- cuts along the mask's own shape, so you get a true cutout with transparency where the mask was empty. Use this for isolating a subject.rectangle- computes the bounding rectangle of the mask and cuts that. This is the one for feeding a sampler, since samplers like rectangles.square_thumb- finds the largest centered square around the masked area. It's a thumbnail mode:paddingandmin_width/min_heightare deliberately ignored, which the docs call out.
output_size then decides the frame: mask_size tightens to the cut region, image_size keeps the original image's dimensions with the surrounding area transparent - useful when you want to keep positional information while only cutting the subject.
Inputs worth knowing
images/mask- the source and the selection.cut_type/output_size- shape and frame, above.round_to- rounds the cut rectangle up to 8/16/32/64px units (only inrectanglemode). This exists because local models often blur at odd dimensions - rounding to 8px units keeps edges crisp. If you're cutting for a model that was trained on 8px multiples, set this instead of fighting blur.padding- expands the cut region by N pixels. Set a little padding so you're not slicing pixels off the subject's edge.min_width/min_height- floor on the cut size, for cases where a tiny mask produces a comically tiny crop.output_alpha- whether the output keeps an alpha channel.
The outputs: image (the cut), mask (the matching mask, for pasting later), and rect (the [x, y, width, height] of the cut region). That rect is the handshake with D2 Paste By Mask - paste the cutout back at the exact same coordinates.
The workflow it enables
The sweet spot is a cut/paste round trip: cut a subject out with rectangle, inpaint or regenerate it elsewhere, paste it back in at the original rect with D2 Paste By Mask, feather the edge, done. It's also a solid way to build a clean subject crop for LoRA training - cut with mask shape and output_alpha on, and you've got transparent cutouts rather than square crops with baked-in background. For that purpose the round_to + padding combo keeps your training crops on the model's native resolution grid.
Installing and troubleshooting
Part of D2 Nodes ComfyUI:
cd ComfyUI/custom_nodes
git clone https://github.com/da2el-ai/D2-nodes-ComfyUI
or search "D2 Nodes ComfyUI" in ComfyUI Manager.
The usual failure is a mask that doesn't match the image - same dimensions are expected, and the node does try to reconcile them, but a mask from a different-size image will cut garbage. If the cut comes out empty, your mask is probably all zeros; check it with a Preview Mask node first. And remember square_thumb ignores your padding and min-size inputs - that's by design, not a bug.
Inputs (9)
| Name | Type | Default | Description |
|---|---|---|---|
| images | IMAGE | — | |
| mask | MASK | — | |
| cut_type | COMBO | 3 options: mask, rectangle, square_thumb | |
| output_size | COMBO | 2 options: mask_size, image_size | |
| round_toopt | COMBO | 5 options: none, 8px, 16px, 32px, 64px | |
| paddingopt | INT | 00–1000 | — |
| min_widthopt | INT | 00–10000 | — |
| min_heightopt | INT | 00–10000 | — |
| output_alphaopt | BOOLEAN | true | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| mask | MASK | — |
| rect | INT | — |