Nodes/ComfyUI-nhknodes/🎯 Crop To Mask (nhk)
ComfyUI Node

🎯 Crop To Mask (nhk)

Tight Crop From a Mask, With the Coordinates to Stitch Back β€” Crop To Mask (nhk)

By EnashkaΒ·Created 12 months agoΒ·Updated 28 days agoΒ· 23
🎯 Crop To Mask (nhk)
  • image
  • mask
  • image
  • mask
  • x
  • y
  • width
  • height
β—„padding32β–Ί
β—„mask_threshold0.50β–Ί
β—„divisible_by1β–Ί

Crop To Mask (nhk) crops an image - and its matching mask - to the bounding box of whatever the mask covers, with one uniform padding value, and hands you the bbox coordinates so you can stitch the crop back into the original frame later. The node's own description nails the intended use: "great after SAM3."

If you've ever run a segmentation model (SAM, SAM2, SAM3, or any mask producer), you know the next problem: the mask tells you where the object is, but you still have to slice it out of the frame at the right resolution for the next step - face detailer, inpaint region, reference crop for a LoRA or an edit model. Doing that by hand means computing boxes, padding, and re-assembly yourself. This node does all of it and remembers the coordinates.

How it works

At execution it binarizes the mask against mask_threshold (0.5 by default: pixels above it count as "object"), finds the tight bounding box, then expands it by padding on all four sides. The crop is clamped to image bounds, so a negative padding (down to -2048) tightens the crop, and a positive one (up to 4096) gives breathing room. Two safety nets are built in: an empty mask above the threshold passes the image through unchanged instead of crashing, and degenerate crops from extreme negative padding fall back to the original too.

The divisible_by option is the one that separates this from a toy: set it to 8, 16, or 64 and the crop width/height get rounded up to a multiple, expanding symmetrically. That matters because many next-stage models (VAEs, samplers, detailers) are picky about dimensions, and it saves you a resize step after cropping.

The outputs are what make stitch-back possible: the cropped image, the cropped mask, and the bbox as x, y, width, height - feed those into a paste/offset node or a compositing node and the crop goes right back where it came from.

Inputs and outputs

  • image - the frame to crop (BHWC).
  • mask - the mask defining the object (MASK). A 2D [H,W] mask gets normalized to 3D automatically.
  • padding - pixels added on all sides; negative tightens. Default 32.
  • mask_threshold - optional, 0.0–1.0, default 0.5.
  • divisible_by - optional, 1–256, default 1 (disabled).

Outputs: image, mask, x, y, width, height.

Installing it

Standard pack install:

cd ComfyUI/custom_nodes && git clone https://github.com/Enashka/ComfyUI-nhknodes

restart, or "NHK Nodes" from ComfyUI Manager. No extra deps.

Where it fits

The classic pipeline is SAM3 β†’ CropToMask β†’ face/hand detail pass on just the object β†’ stitch back with the bbox. One honest note: it uses the first mask in the batch to define the bbox, which is right for the typical single-subject SAM case but wrong if your batch has multiple different objects. And remember the inpainting context here - if you crop to a mask and then inpaint, the divisible_by rounding keeps your region friendly to the sampler, but you still want to re-blend at stitch-back rather than pasting a hard rectangle. For a "segment and process just that thing" workflow, this is the cleanest single node for it in this pack.

Categorynhk/image

Inputs (5)

NameTypeDefaultDescription
imageIMAGEImage to crop (BHWC)
maskMASKMask defining the object bounding box
paddingINT32-2048–4096Pixels added on all sides of the mask bbox. Negative values tighten the crop.
mask_thresholdoptFLOAT0.500–1Pixels with mask value above this count as 'object' when computing the bbox
divisible_byoptINT11–256Round the crop width/height up to a multiple of this value (e.g. 8, 16, 64). 1 = disabled.

Outputs (6)

NameTypeDescription
imageIMAGEβ€”
maskMASKβ€”
xINTβ€”
yINTβ€”
widthINTβ€”
heightINTβ€”