CropByMaskToSpecificSize
CropByMaskToSpecificSize
- image
- mask
- crop_box
- croped_image
- croped_mask
- crop_box
- box_preview
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. Checkbox_previewrather 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
SplitMasknode first to isolate the region you actually want cropped. - Feeding a previously computed
crop_boxback in for consistency only works cleanly if the source image dimensions haven't changed between runs - acrop_boxcomputed against one image size won't translate correctly onto a differently sized image.
Inputs (12)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| invert_mask | BOOLEAN | false | — |
| top_reserve | FLOAT | 0.100–1 | — |
| bottom_reserve | FLOAT | 0.100–1 | — |
| left_reserve | FLOAT | 0.100–1 | — |
| right_reserve | FLOAT | 0.100–1 | — |
| width | INT | 1024200–4096 | — |
| height | INT | 1024200–4096 | — |
| width_padding_position | COMBO | center | 3 options: left, center, right |
| height_padding_position | COMBO | center | 3 options: top, center, bottom |
| crop_boxopt | BOX | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| croped_image | IMAGE | — |
| croped_mask | MASK | — |
| crop_box | BOX | — |
| box_preview | IMAGE | — |