Image Crop by Mask
Crop precisely around a mask, then feed it to an inpaint pass
- image
- mask
- image
- mask
The KB's masking doc puts it in one sentence: "All detailers are doing is zooming in on a mask, upscaling the image, and then diffusing the upscaled image." Step two of that loop - cut the masked region out and give it to the model at a resolution it can actually resolve - is exactly what Image Crop by Mask does. You feed it an image and a mask; it computes the mask's bounding box, crops that region out, and resizes it to your target dimensions.
Its natural home is inpainting pre-processing (the author says so outright). The classic build: generate a mask (painted, or from a detector), crop the region to a workable size, run your inpaint pass on the crop, then paste the result back over the original. Doing the crop yourself instead of letting the model see the whole frame is what keeps a 70-pixel face from coming out as a smear.
The inputs that shape the crop
Most of these you can leave alone, but three are worth knowing:
- mask_expand - dilates the mask before the bounding box is computed. Your mask hugs the subject; expanding it a few pixels keeps the crop from cutting through edges and gives the inpaint breathing room.
- context_expand (1.0–4.0) - grows the crop box by a factor. 1.0 is a tight crop; 2.0 gives you a 2× bigger frame. This is the "don't crop so tight the model has no context" dial, and it matters more than people expect.
- mask_threshold (default 0.1) - zeros out mask values below the threshold, so faint mask noise doesn't blow up the bounding box.
The rotation and mirror inputs rotate/mirror the image and mask together before cropping - the mask stays registered with the pixels, which is the part that usually breaks when people hand-roll this. And there's a padding combo that snaps the output size to a multiple (default 32), plus target_width/target_height (defaults 512×512), rescale_algorithm, and a device selector that defaults to auto (GPU if available).
Outputs are the cropped-resized image and mask, both as lists, both resized to the same target - so they line up perfectly for a masked generation or a paste-back.
Where it hurts to skip
If you've ever watched a detailer fumble because the crop box was jagged or off-center, this is the fix. It's the preprocessing half of the Impact-Pack-style loop, minus the detector - you bring the mask, it brings the geometry.
Installing
ComfyUI_Eclipse, r-vage's pack. Manager → search ComfyUI_Eclipse, or:
cd ComfyUI/custom_nodes
git clone https://github.com/r-vage/ComfyUI_Eclipse
Restart; it's under Eclipse → Image → Transforms. Standard deps (torch, numpy, Pillow, opencv-python). Eclipse targets current ComfyUI - frontend 1.51.2+ for the modern widgetry, v4.0 removed legacy RvTools_v2 nodes with a Migration Tool for old saves.
Start with context_expand around 1.5 and mask_expand of a few pixels; tune from there. And if your inpaint output has hard seams, that's the paste-back job, not this node - crop-by-mask gets the geometry right so the rest of the loop can do its thing.
Inputs (12)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | Source image to crop. | |
| mask | MASK | Mask defining the region of interest. | |
| rotation | INT | 0-180–180 | Rotate input image and mask by this angle (degrees) before cropping. Positive = clockwise, negative = counter-clockwise. |
| mirror | COMBO | none | Mirror input image and mask before cropping. |
| mask_expand | INT | 00–512 | Dilate mask by this many pixels before computing bounding box. |
| mask_threshold | FLOAT | 0.100–1 | Zero out mask values below this threshold (hi-pass filter). |
| context_expand | FLOAT | 1.001–4 | Grow the crop bounding box by this factor (1.0 = tight crop, 2.0 = 2x size). |
| target_width | INT | 51264–16384 | Resize cropped region to this width. |
| target_height | INT | 51264–16384 | Resize cropped region to this height. |
| padding | COMBO | 32 | Snap output dimensions to this multiple. |
| rescale_algorithm | COMBO | bicubic | Interpolation method for resize. |
| device | COMBO | auto | Processing device. 'auto' uses GPU if available. |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | Cropped and resized image region. |
| mask | MASK | Cropped and resized mask. |