ComfyUI Node

Crop From Mask

Let the mask find the subject — auto-crop to a bounding box with breathing room

By Pirog17000·Created about a year ago·Updated 10 months ago· 3
Crop From Mask
  • images
  • masks
  • cropped_images
  • bboxes
expansion0
use_binary_masktrue
threshold0.50

The manual crop node is for when you know the region. Crop From Mask is for when you don't - you have a mask (from a seg model, a face detector, an inpaint mask, whatever), and you want the smallest box that contains it, with a little padding so the crop doesn't hug the subject like a shrink-wrap. This is the workhorse of the pack's crop-uncrop pipeline, and it's the node you'll reach for when you want to give one region of an image its own generation budget.

How it works

For each image/mask pair it scans the mask for active pixels, finds the bounding box that covers them, and crops the image to that box. Three knobs control what "active" means:

  • expansion (0–512 px) - pads the bbox on all sides so the crop includes context around the subject. This is where you add "breathing room."
  • use_binary_mask (default on) - thresholds the mask into solid active/inactive before measuring, using threshold (default 0.5). With it off, it uses the mask's grayscale and treats anything above 0.5 as active - same thing for most masks, but the binary path is cleaner with soft or noisy masks.
  • threshold - the cutoff when binary mode is on.

Outputs: cropped_images (an IMAGE batch) and bboxes (the (x, y, w, h) coordinates for each crop). Those bboxes are the whole point - they're what lets the uncrop node put everything back.

Why you'd build this pipeline

The classic use is crop-and-process: take a mask of a face, crop just the face region, run a high-detail img2img pass on it at the model's native resolution, then paste it back. The KB's inpainting notes make the value concrete - a 64px eye gets a 1024px generation budget when you crop it first. Same idea applies to upscaling a subject separately from its background.

Installing it

Ships in Pirog's Nodes:

cd ComfyUI/custom_nodes
git clone https://github.com/Pirog17000/Pirogs-Nodes
pip install -r Pirogs-Nodes/requirements.txt

Or search "Pirog's Nodes" in ComfyUI Manager and restart. Pure torch - no extra dependencies.

Gotchas

Two things to watch. First, if a mask comes back with zero active pixels, the node skips that image's crop - and if all of them are empty, it falls back to returning the original image with a full-size bbox rather than erroring. That silent fallback is sensible but easy to miss; if your output looks uncropped, check your mask. Second, remember the bbox measures the mask's extent plus expansion - a mask that bleeds to the image edge produces a crop that includes the edge, so leave yourself some mask margin if you want a centered subject.

Categorypirog/image

Inputs (5)

NameTypeDefaultDescription
imagesIMAGE
masksMASK
expansionINT00–512
use_binary_maskBOOLEANtrue
thresholdFLOAT0.500–1

Outputs (2)

NameTypeDescription
cropped_imagesIMAGE
bboxesBBOX