Nodes/KJNodes for ComfyUI/Image Crop By Mask
ComfyUI Node Runs on cloud

Image Crop By Mask

Crop an image down to whatever your mask covers

By kijai·Created 3 years ago·Updated about 14 hours ago· 2,930
Image Crop By Mask
  • image
  • mask
  • image

You've got a mask - from a segmentation node, a detector, a hand-painted region - and you want to throw away everything the mask doesn't cover and just keep the box around it. That's the whole job of ImageCropByMask. It takes your image, looks at where the mask is white, and hands you back the image cropped to that region.

Why bother? Because a lot of work is cheaper and better when you do it on a crop instead of the full frame. If you're detailing a face that's 200px in a 1500px image, running your detailer on the whole thing wastes compute and often gives the model too much context to wander in. Crop to the face, work on it, paste it back - that's the pattern, and this node is the "crop to it" half. It's the same idea behind every face-detailer and inpaint-and-stitch flow, just as a plain, do-one-thing utility you can drop anywhere.

How it works

Nothing exotic. It reads the mask, finds the bounding box of the masked (white) area, and slices the image to that rectangle. The mask defines where, the image is what gets cut. If your mask is a tight circle around a dog, you get a rectangle snug around the dog.

That simplicity is the point. Kijai's KJNodes pack is, in the author's own words, a "pretty random collection of utility, model optimization and QoL nodes" - and this is squarely a QoL node. It doesn't try to smooth, pad, or animate anything. Need those? There are heavier siblings in the same pack for that.

The inputs and outputs that matter

There are only two inputs, and both are required:

  • image (IMAGE) - what you want cropped.
  • mask (MASK) - where the crop lands. White = keep, and the node crops to the box that contains the white.

The single output is image (IMAGE) - the cropped result, which you wire straight into whatever comes next: a sampler, an upscaler, a preview.

Make sure the mask lines up with the image it's describing. Same source, same dimensions. A mask generated for a different resolution than the image will give you a crop in the wrong place, and it won't error - it'll just be wrong, which is worse.

How to install it

Easiest path is ComfyUI Manager: open it, search KJNodes for ComfyUI, install, restart. Done.

Manual works too:

cd ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-KJNodes
pip install -r ComfyUI-KJNodes/requirements.txt

(On a Windows portable install, run the pip line with python_embeded\python.exe -m pip install -r ... instead.) Restart ComfyUI and the node shows up under KJNodes/image. No model downloads, no heavy dependencies - the pack deliberately keeps requirements minimal.

Common issues & troubleshooting

"The crop is huge / off-center." That's a stray white pixel in your mask. The bounding box stretches to include every white pixel, so one speck in the corner blows the box out to the whole frame. Clean the mask first - a GrowMask/shrink or a threshold pass upstream fixes it.

"I need padding around the subject." ImageCropByMask crops tight to the mask box, no margin. If you want breathing room or a fixed output size, reach for one of the BatchCropFromMask nodes in this same pack, which give you a crop-size multiplier and bbox smoothing.

"I want to paste it back later." This node only crops - it doesn't hand you the coordinates to stitch the result back into the original. For a full crop-work-stitch loop across a batch, the BatchCropFromMask family outputs bounding boxes you can feed to a paste-back node. Use this one when the crop is the destination, not a round trip.

CategoryKJNodes/image

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
maskMASK

Outputs (1)

NameTypeDescription
imageIMAGE