Nodes/ComfyUI-Practical-Tools/Image Crop By Mask
ComfyUI Node

Image Crop By Mask

Crop to the subject, not to a box you eyeballed

By wenchengxiang·Created about a month ago·Updated a day ago· 1
Image Crop By Mask
  • image
  • mask
  • IMAGE
  • IMAGE_BOUNDS
padding_left64
padding_right64
padding_top64
padding_bottom64

The crop-and-stitch pattern is one of the most important in ComfyUI: cut a region out of a big image, do real work on it (inpaint, upscale, detail), then paste it back. And the whole thing starts with a good crop. Image Crop By Mask is that first step - it looks at a mask, finds the bounding box of everything the mask marks, and crops the image to exactly that, with a padding you control on each side.

The mechanism is exactly what you'd hope: for each frame, it finds the outermost nonzero mask pixels, expands the box by your per-side padding, and slices the image. The IMAGE_BOUNDS output is the important part - it's not pixels, it's the four numbers describing where the crop came from, and that's what lets another node put it back. The pack ships the matching node for that: Image Uncrop By Bound, which takes your crop, the bounds, and the original image, and composites it back with an optional feather and blend factor. Together they're a self-contained crop → process → stitch loop with no external dependencies.

Defaults matter here: each padding side defaults to 64px, and the inpainting literature lands right in that range - "only masked" region fixes want 32–64px of padding so the sampler has context to blend against. Crank it up if your detailer needs more surrounding pixels; drop it if you want a tight crop for upscaling a face.

Inputs and outputs that matter

  • image (in) - the full image.
  • mask (in) - what to crop around.
  • padding_left / right / top / bottom (in) - expansion on each side, default 64px each, up to ~1M.
  • IMAGE (out) - the cropped region.
  • IMAGE_BOUNDS (out) - the crop rectangle, feed this (plus the original image) into Image Uncrop By Bound to paste back.

Installing it

Part of wenchengxiang/ComfyUI-Practical-Tools. ComfyUI Manager → Custom Nodes Manager → search "ComfyUI-Practical-Tools" → Install → restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/wenchengxiang/ComfyUI-Practical-Tools

then restart. No pip dependencies, no model files - pure Python on ComfyUI's bundled stack.

Issues and gotchas

One safety behavior to know: if a mask frame is entirely black (no region found), the node reuses the previous frame's bounds instead of crashing - handy for batches, surprising if you were expecting an error. The bounds are stored as (rmin, rmax, cmin, cmax), and Image Uncrop By Bound expects them in that exact shape, so keep the two nodes wired together rather than reconstructing bounds by hand. And the crop is tight to the mask plus padding - it won't snap to nice numbers, so don't be startled by odd crop dimensions like 247×189. That's normal, and it's why the stitch step exists.

CategoryPractical-Tools/Image

Inputs (6)

NameTypeDefaultDescription
imageIMAGE
maskMASK
padding_leftINT640–999999
padding_rightINT640–999999
padding_topINT640–999999
padding_bottomINT640–999999

Outputs (2)

NameTypeDescription
IMAGEIMAGE
IMAGE_BOUNDSIMAGE_BOUNDS