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

Image Crop By Mask And Resize

Zoom into a masked region for detail work

By kijai·Created 3 years ago·Updated about 10 hours ago· 2,930
Image Crop By Mask And Resize
  • image
  • mask
  • images
  • masks
  • bbox
base_resolution512
padding0
min_crop_resolution128
max_crop_resolution512

This is the "crop in on the thing I care about, fix it, paste it back" node. You've got a mask marking a region - a face, a hand, a logo - and you want to work on just that area at higher resolution instead of re-rendering the whole image. ImageCropByMaskAndResize finds the mask's bounding box, crops the image to it, and resizes that crop up to a working resolution. The idea is the same one behind Impact Pack's FaceDetailer: small details get more pixels and therefore more model attention when you isolate and upscale them before processing.

How it works

It reads your mask, computes the tight rectangle that contains the masked area, optionally expands that rectangle by some padding, crops the image to it, and scales the result to a target resolution. Crucially it also gives you back the bounding box coordinates, so after you've refined the crop you can paste it back into the original at the right spot.

The inputs that matter

  • image and mask - the picture and the region to crop to. The mask drives everything.
  • base_resolution - the size the crop gets resized to (default 512). This is why the technique works: a tiny face in a full scene might be 80 pixels tall; blow that crop up to 512 and your sampler has real resolution to work with.
  • padding - extra pixels around the mask's box, so you don't crop right to the edge of the subject and lose context the model needs to blend cleanly.
  • min_crop_resolution / max_crop_resolution - clamps on how small or large the crop can be before resizing, keeping things in a sane range regardless of how big or tiny the masked area is.

The outputs are the cropped images, the cropped masks (aligned to the new crop), and the bbox - a BBOX carrying the crop's position and size. That bbox is the important one: feed it to a stitch-back node so the refined region lands exactly where it came from.

The typical flow

Detect or draw a mask → crop with this node → run your detail pass (a low-denoise resample, an upscale, a different model) on the small high-res crop → paste back using the bbox. It's the standard "detailer" pattern, and it's how you fix faces and hands without the cost and drift of re-rendering the entire frame.

Installing KJNodes

ComfyUI Manager: Custom Nodes Manager, search "KJNodes for ComfyUI", install, restart. Manual: cd ComfyUI/custom_nodes && git clone https://github.com/kijai/ComfyUI-KJNodes, then pip install -r ComfyUI-KJNodes/requirements.txt (portable: python_embeded\python.exe -m pip install -r ComfyUI\custom_nodes\ComfyUI-KJNodes\requirements.txt), restart. No downloads.

Common issues

Too little padding is the usual complaint - crop tight to a face with zero padding and the pasted-back result has a visible seam, because the model had no surrounding context to match skin tone and lighting. Give it some breathing room.

The other one is expecting the node to paste back on its own. It doesn't; it only crops and hands you the bbox. You need a corresponding paste/stitch step downstream that consumes that bbox. If your refined detail vanishes or ends up in the wrong place, it's because the bounding box wasn't carried through to the paste. And if the crop is a wildly wrong size, that's what min_crop_resolution and max_crop_resolution are there to rein in.

CategoryKJNodes/image

Inputs (6)

NameTypeDefaultDescription
imageIMAGE
maskMASK
base_resolutionINT5120–16384
paddingINT00–16384
min_crop_resolutionINT1280–16384
max_crop_resolutionINT5120–16384

Outputs (3)

NameTypeDescription
imagesIMAGE
masksMASK
bboxBBOX