Nodes/ComfyUI-Image-Inpainting/Crop Image by Rect
ComfyUI Node

Crop Image by Rect

The depth-map crop nobody searches for, and why it matters

By SherryXieYuchen·Created 2 years ago·Updated 2 years ago· 4
Crop Image by Rect
  • original_image
  • image
  • rect
  • IMAGE

"Crop Image by Rect" looks like a pointless sibling of INPAINT_CropImage until you see what the bundled workflow uses it for, and then it clicks: it's the node that lets you crop a processed derivative of the image to the same rectangle as the original crop. In this pack, that derivative is a depth map.

Here's the scenario. You cropped a region for inpainting and got a RECT back. Now you also want a depth ControlNet guiding the diffusion pass - so you compute a depth map of the whole frame, and you need to crop the depth map down to that same rectangle, at the same resolution, so it lines up pixel-perfectly with the latent you're sampling. INPAINT_CropImage can't do it, because it finds its own bounding box from a mask. This node doesn't: it just takes a rect and slices.

The depth ControlNet is what makes the re-render in the bundled workflow respect the scene - the KB's controlnet essay is explicit that depth preserves spatial arrangement, which is exactly what you want when you're telling the model "regenerate this region as an empty background, but keep the geometry." Crop the depth map to the crop, feed it to ControlNetApplyAdvanced, and the structure survives the regeneration.

How it works

Three inputs: original_image, image, and rect. First it resizes image to match original_image's dimensions (that's the part people miss - the processed derivative is frequently a different resolution than the source), then it crops using the rect's (x_min, y_min, x_max, y_max) and returns the slice as an IMAGE.

In the workflow: image is the Zoe depth map, original_image is the source frame, and rect comes from the same INPAINT_CropImage that produced the image crop. The output feeds the ControlNet's image input. One node doing one job, and the wiring is the whole story.

Install

The same pack install as everything else in SherryXieYuchen/ComfyUI-Image-Inpainting:

cd ComfyUI/custom_nodes
git clone https://github.com/SherryXieYuchen/ComfyUI-Image-Inpainting
# restart ComfyUI

(Or Manager → "ComfyUI-Image-Inpainting".) No models of its own - but if you're using it the way the workflow intends, you need a depth estimator (the README lists ZoeDepth, ZoeD_M12_N.pt, via comfyui_controlnet_aux) and the depth ControlNet (control_v11f1p_sd15_depth.pth).

Gotchas

  • original_image drives the resize, so it should be the source frame, not the processed one. That's the whole point of the resize step.
  • It's single-image; squeeze(0) everywhere, so no batches.
  • The resize is plain cv2.resize - if your derivative and your source have very different aspect handling, verify the rect still aligns visually before trusting the output. When it's wrong it's usually because the derivative was already cropped once.
Categoryimage

Inputs (3)

NameTypeDefaultDescription
original_imageIMAGE
imageIMAGE
rectRECT

Outputs (1)

NameTypeDescription
IMAGEIMAGE