Nodes/zhangp365/ComfyUI-utils-nodes/CropByMaskToSpecificSize
ComfyUI Node

CropByMaskToSpecificSize

CropByMaskToSpecificSize

By zhangp365·Created 2 years ago·Updated 5 months ago· 134
CropByMaskToSpecificSize
  • image
  • mask
  • crop_box
  • croped_image
  • croped_mask
  • crop_box
  • box_preview
invert_maskfalse
top_reserve0.10
bottom_reserve0.10
left_reserve0.10
right_reserve0.10
width1024
height1024
width_padding_positioncenter
height_padding_positioncenter

Most "crop by mask" nodes just give you the tightest bounding box around your masked region, at whatever size that happens to be. This one goes a step further: it crops to a mask and forces the result to an exact target size you specify - which matters a lot if whatever comes next in your graph (a fixed-resolution model, a batch of crops that need to match dimensions, a face-restoration pass) needs a consistent input size rather than whatever the mask's natural bounding box produced.

The core inputs: image and mask are required, along with width and height (both default 1024, range 200–4096) - your target output dimensions. invert_mask (off by default) flips which side of the mask you're cropping to, for when your mask covers what you want to exclude rather than what you want to keep. The four *_reserve fields (top_reserve, bottom_reserve, left_reserve, right_reserve, each default 0.1, range 0–1) add margin around the mask's actual bounding box before cropping - a value of 0.1 means roughly 10% extra padding on that side relative to the mask's extent, so you're not cropping right up against the mask edge with zero breathing room. width_padding_position and height_padding_position (each left/center/right or top/center/bottom, defaulting to center) control where the extra padding goes when the masked region's aspect ratio doesn't match your target width/height - center-padding is the safe default for most subjects, but if your subject sits off-center in the mask, adjusting these keeps more of it in frame instead of centering the crop and cutting off part of what you actually wanted.

Outputs: croped_image and croped_mask are your result, sized exactly to width×height. crop_box is the computed crop coordinates as a BOX type - feed this into the optional crop_box input on a second run (of this node, or paste it back with a composite node) to reuse the exact same crop region rather than recomputing it, which is the mechanism you'd use to crop, process, then paste a result back into the identical location. box_preview gives you an IMAGE showing the crop region drawn over your original - genuinely useful for sanity-checking that the crop landed where you expected before you commit an entire batch to it.

Install: search "ComfyUI-utils-nodes" in ComfyUI Manager, or git clone https://github.com/zhangp365/ComfyUI-utils-nodes into custom_nodes, then restart. No models or API keys needed - pure image math.

Common issues:

  • If your masked region's aspect ratio is very different from your target width/height, the reserve and padding-position settings do a lot of work to reconcile the two - expect some of the surrounding image (not just the mask) to get pulled into frame to fill out the target dimensions. Check box_preview rather than assuming the crop only ever touches exactly the masked pixels.
  • A mask with disconnected regions doesn't have a single well-defined bounding box the way a single blob does - if your mask covers more than one separate area, look at this pack's SplitMask node first to isolate the region you actually want cropped.
  • Feeding a previously computed crop_box back in for consistency only works cleanly if the source image dimensions haven't changed between runs - a crop_box computed against one image size won't translate correctly onto a differently sized image.
Categoryutils/mask

Inputs (12)

NameTypeDefaultDescription
imageIMAGE
maskMASK
invert_maskBOOLEANfalse
top_reserveFLOAT0.100–1
bottom_reserveFLOAT0.100–1
left_reserveFLOAT0.100–1
right_reserveFLOAT0.100–1
widthINT1024200–4096
heightINT1024200–4096
width_padding_positionCOMBOcenter3 options: left, center, right
height_padding_positionCOMBOcenter3 options: top, center, bottom
crop_boxoptBOX

Outputs (4)

NameTypeDescription
croped_imageIMAGE
croped_maskMASK
crop_boxBOX
box_previewIMAGE