Image Crop With BBox Mask
Crop around your subject's mask — and remember where it came from
- image
- mask
- get_crop_ratio
- cropped_image
- bbox_mask
- cropped_mask
The "crop, process, paste back" loop is the backbone of inpainting and local upscaling: find the subject, crop tightly around it, run the expensive model on that crop, then put the result back where it came from. The crop part is easy. The putting it back part is where workflows die, because you need to remember the exact bounding box. Image Crop With BBox Mask solves both halves: it crops intelligently around your mask's bounding box, and it hands you back a bbox_mask that records the crop's position in the original image - the key to a clean paste-back.
This is a genuinely useful node for inpainting prep, face fixes, dataset building, and anything that needs "subject extracted at a known location."
How it works
The node takes the mask's bounding box, then shapes the crop around it:
- preset_ratio - the target aspect ratio:
mask(fits the subject's own bbox ratio closely),image(original image ratio),auto(snaps to the nearest standard ratio like 4:3 or 16:9), or an explicit ratio from the list. Defaultmask, which is right for most subjects. - get_crop_ratio - an optional reference image that overrides
preset_ratioentirely: the crop matches this image's aspect. Handy when you want all crops to match a fixed canvas. - scale_strength - 0–1. Extends the crop box outward while keeping the ratio, pulling in more background context. 0 is a tight fit; higher values stop the subject from touching the crop edges.
- crop_to_side + crop_to_length - physical output size control: fix the
longest,shortest,width, orheightto a length (e.g. long side = 1024). - divisible_by - rounds output dimensions to a multiple (default 8; 16 for some models), so the crop lands on model-friendly sizes.
Outputs are the trio that makes paste-back work:
- cropped_image - the crop, resized to the final size.
- bbox_mask - a mask in original image dimensions; white marks exactly where the crop happened. This is the "map" for restoring.
- cropped_mask - your input mask cropped and resized to match
cropped_image, for aligned downstream compositing.
Install
Part of ComfyUI-1hewNodes:
cd ComfyUI/custom_nodes
git clone https://github.com/1hew/ComfyUI-1hewNodes
Restart ComfyUI. No models, no downloads.
Where it fits
The canonical loop: mask the subject (matting, or a SAM box) → crop here → inpaint or upscale the crop → paste back using bbox_mask - the pack's Image Paste by BBox Mask is built for exactly that handoff. Also solid for dataset prep: batch-crop subjects to a uniform 1024×1024 with crop_to_side + divisible_by.
Common issues
get_crop_ratioconnected and nothing changed when you tweakedpreset_ratio- by design, the reference image wins. Disconnect it to use the ratio presets.- Subject touches the edge -
scale_strengthis your knob; 0.1–0.2 usually adds enough breathing room. - Empty or invalid mask - the node returns default black/white outputs instead of crashing, so a dead branch won't kill the queue. Spot-check if you see a black crop.
If you've ever hand-jotted down crop coordinates to restore an image, this node is the version of that chore that never makes arithmetic mistakes.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| preset_ratio | COMBO | mask | 13 options: mask, image, auto, 9:16, 2:3, 3:4, +7 |
| scale_strength | FLOAT | 0.000–1 | — |
| crop_to_side | COMBO | None | 5 options: None, longest, shortest, width, height |
| crop_to_length | INT | 10248–8192 | — |
| divisible_by | INT | 81–1024 | — |
| get_crop_ratioopt | IMAGE | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| cropped_image | IMAGE | — |
| bbox_mask | MASK | — |
| cropped_mask | MASK | — |