Nodes/D2 Nodes ComfyUI/D2 Cut By Mask
ComfyUI Node

D2 Cut By Mask

Cut an object out of an image with a mask, cleanly

By da2el-ai·Created 2 years ago·Updated 5 days ago· 66
D2 Cut By Mask
  • images
  • mask
  • image
  • mask
  • rect
cut_type
output_size
round_to
padding0
min_width0
min_height0
output_alphatrue

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: padding and min_width/min_height are 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 in rectangle mode). 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.

CategoryD2/Image

Inputs (9)

NameTypeDefaultDescription
imagesIMAGE
maskMASK
cut_typeCOMBO3 options: mask, rectangle, square_thumb
output_sizeCOMBO2 options: mask_size, image_size
round_tooptCOMBO5 options: none, 8px, 16px, 32px, 64px
paddingoptINT00–1000
min_widthoptINT00–10000
min_heightoptINT00–10000
output_alphaoptBOOLEANtrue

Outputs (3)

NameTypeDescription
imageIMAGE
maskMASK
rectINT