Crop Image Inpaint
The 'only masked' inpainting trick, wrapped in one node
- image
- mask
- IMAGE
- MASK
- RECT
"Crop Image Inpaint" is the opening move of this pack's crop-and-stitch pipeline, and it solves the single most important inpainting performance trick: don't send the whole image to the model when you only need to fix a small region. It looks at your mask, finds the bounding box of what's actually marked, and crops the image (and mask) down to just that box, with a little breathing room around it.
This is the old "only masked" strategy from the A1111 days, and it still matters because it's the reason small fixes look good. Inpaint a face or a removed object at the model's full resolution within the crop and you've spent all that generation budget on the thing you care about, instead of squandering it on 90% background. The KB's inpainting essay calls it out directly: for fixing small details, "only masked" with adequate padding produces the best results.
How it works
Three inputs: image, mask, and pad_ratio. The node converts the mask to a numpy array, runs cv2.findContours over it, collects every contour's points, and takes the single boundingRect of the union. So multiple mask blobs still get one box around all of them - worth knowing if you've painted two separate areas and expected two crops.
pad_ratio (default 0.1) decides the margin: the box is extended by half that fraction of its width and height on each side. The bundled workflow uses 0.2. More padding means more context for the model to blend against, which is almost always good - the classic mistake is 0 padding and then wondering why the inpainted patch looks glued on. The padding is clamped at the image edges, so you can't crop past the border.
Outputs are the three things downstream needs: the cropped IMAGE, the cropped MASK, and the RECT - a tuple of (x_min, y_min, x_max, y_max) that records exactly where the crop came from. That RECT is the pack's glue: Inpainting (using Model) doesn't use it, but Crop Image by Rect and Paste Back Crop Image both live and die by it, because they need to know where the crop lives so they can put processed versions back in the same spot.
Install
The usual for this pack - ComfyUI Manager → search "ComfyUI-Image-Inpainting", or:
cd ComfyUI/custom_nodes
git clone https://github.com/SherryXieYuchen/ComfyUI-Image-Inpainting
# restart ComfyUI
No models needed for this node on its own; it's pure OpenCV numpy.
Gotchas
- Single image in, single image out. Everything runs through
squeeze(0); feed a batch and you'll get nonsense. Handle one frame at a time. - White in the mask is the region. The crop follows the bright parts of the mask - if your mask is inverted, you'll crop the wrong area.
- The crop is a rectangle, not a contour. Fine for inpainting; just don't expect an alpha-matte tight to your brush strokes.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| pad_ratio | FLOAT | 0.100–1 | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |
| MASK | MASK | — |
| RECT | RECT | — |