Nodes/ComfyUI-YogurtNodes/Image Crop By Mask (Yogurt Nodes)
ComfyUI Node

Image Crop By Mask (Yogurt Nodes)

Crop exactly to the mask — and get the coordinates back

By yogurt7771·Created 2 years ago·Updated 9 days ago· 1
Image Crop By Mask (Yogurt Nodes)
  • image
  • mask
  • cropped_image
  • x
  • y
  • width
  • height
threshold0.50

When you inpaint a face at full-image resolution, you're wasting generation budget - the model has to work around a 1024px canvas to change a 64px eye. The standard trick, straight out of every serious inpaint workflow: crop the masked region tight, render it at native resolution, paste it back. Image Crop By Mask is the crop half of that deal, and it does the one thing most crop nodes forget - it hands you the coordinates so you can paste back without guessing.

It's an image node from the YogurtNodes pack (yogurt7771/ComfyUI-YogurtNodes), and it's the perfect partner for the pack's repaint/region nodes if you're building the crop-and-stitch pipeline by hand.

How it works

You give it an image and a mask. It thresholds the mask at threshold (default 0.5), finds the minimum bounding box that contains every pixel above that threshold, and crops the image to that box. Simple, but the return values are the clever part: alongside cropped_image you get x, y, width, height - the exact pixel position and size of the crop in the original.

Those four ints are what make it composable. Inpaint the crop, and you can paste the result back at (x, y) with a mask, and it lands exactly where it came from. That's the difference between a crop-and-stitch workflow that works and one where things drift by a few pixels every iteration.

Inputs that matter

  • image - what you're cropping.
  • mask - what defines the region. A segmentation mask, an attention mask, whatever you have.
  • threshold - how strict the region boundary is. At 0.5, only pixels brighter than mid-gray count. If your mask is soft/feathered and you feel the crop is too tight, drop the threshold to pull in more of the halo.

Outputs: cropped_image, x, y, width, height.

Install

Same clone as every Yogurt node:

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

Or ComfyUI Manager → search "YogurtNodes". Restart, and you'll find it under Yogurt Nodes / Image. No models, no downloads - pure numpy/PIL math.

Troubleshooting

The usual suspects:

  • "The crop is bigger than the mask." A soft or feathered mask has a gradual falloff, so pixels stay above threshold farther out than you'd think. Either threshold higher or erode the mask before it gets here.
  • "The crop is empty / tiny." If your mask is all below threshold, the bounding box collapses to a point or the node errors on an empty region. Normalize your mask to 0–1 first.
  • Keeping it aligned. The killer mistake is forgetting the coordinates when you paste back. Keep x/y wired through your graph - don't hardcode them, because the crop moves every time the mask does.
  • Batch handling. It crops per batch item, so a batch of images + a batch of masks gives you a batch of crops. If masks and images are different sizes, that's a mismatch - resize the mask to the image before feeding it.

The clean mental model: this node is "select the important region" in the crop-and-stitch pipeline. Pair it with an inpaint pass and an image paste node and you've got the whole detail-restoration loop without ever leaving the graph.

CategoryYogurtNodes/Image

Inputs (3)

NameTypeDefaultDescription
imageIMAGEThe image to crop.
maskMASKThe mask to crop by.
thresholdFLOAT0.500–1Threshold for mask values to be considered valid.

Outputs (5)

NameTypeDescription
cropped_imageIMAGE
xINT
yINT
widthINT
heightINT