Nodes/ComfyUI_Eclipse/Image Crop by Mask
ComfyUI Node

Image Crop by Mask

Crop precisely around a mask, then feed it to an inpaint pass

By r-vage·Created 10 months ago·Updated a day ago· 31
Image Crop by Mask
  • image
  • mask
  • image
  • mask
rotation0
mirrornone
mask_expand0
mask_threshold0.10
context_expand1.00
target_width512
target_height512
padding32
rescale_algorithmbicubic
deviceauto

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.

Category🌒 Eclipse/ Image/Transforms

Inputs (12)

NameTypeDefaultDescription
imageIMAGESource image to crop.
maskMASKMask defining the region of interest.
rotationINT0-180–180Rotate input image and mask by this angle (degrees) before cropping. Positive = clockwise, negative = counter-clockwise.
mirrorCOMBOnoneMirror input image and mask before cropping.
mask_expandINT00–512Dilate mask by this many pixels before computing bounding box.
mask_thresholdFLOAT0.100–1Zero out mask values below this threshold (hi-pass filter).
context_expandFLOAT1.001–4Grow the crop bounding box by this factor (1.0 = tight crop, 2.0 = 2x size).
target_widthINT51264–16384Resize cropped region to this width.
target_heightINT51264–16384Resize cropped region to this height.
paddingCOMBO32Snap output dimensions to this multiple.
rescale_algorithmCOMBObicubicInterpolation method for resize.
deviceCOMBOautoProcessing device. 'auto' uses GPU if available.

Outputs (2)

NameTypeDescription
imageIMAGECropped and resized image region.
maskMASKCropped and resized mask.