ComfyUI Node

MaskToCrop

Crop whatever your mask points at, perfectly centered

By Felsir·Created 3 years ago·Updated about a year ago· 23
MaskToCrop
  • image_in
  • mask_in
  • IMAGE
  • rect_left
  • rect_top
  • rect_width
  • rect_height
resolution512
fit_insidefalse
force_squaretrue
padding0

Mask to Crop answers the question "the thing I care about is somewhere in this image - cut it out for me." Give it an image and a mask, and it finds the masked region, centers a crop on it, and scales the result to a resolution you pick. It's the missing piece for anyone doing detect-and-crop work who doesn't want to hand-position a single crop box.

This is the "crop" half of the detect-crop-refine loop from the ComfyUI detection ecosystem - the same loop where detectors emit masks and detailers refine regions. If you have a mask but you don't want the whole detailing machinery, you just want the region as a standalone image for an img2img pass or to drop into a new composition, this node is that step.

How it works

The node reads the mask and finds every pixel above 0.5. From those it computes two things: the center and the bounding box. The bbox becomes the crop's extent (with padding added), the center becomes the focal point, and then it hands off to the same crop-and-scale logic that Focal Rescale Rel uses internally. You get relative-coordinate cropping with the coordinates figured out for you.

The inputs that matter

  • mask_in - any MASK. The region it crops is where the mask is ≥ 0.5.
  • force_square (default true) - makes the output square at resolution × resolution. Uncheck it and the crop keeps the mask's own aspect ratio instead, which matters when the subject is tall or wide.
  • resolution - the output size (16–2048). The square side when force_square is on; the long side when it's off.
  • padding - extra margin around the mask's bbox, in pixels. Give a detected face some shoulder room instead of a tight chin crop.
  • fit_inside - same meaning as in Focal Rescale Rel: clamp the crop inside the image (on) or let it bleed to black (off). Keep it on.

Outputs: the cropped IMAGE plus rect_left / rect_top / rect_width / rect_height in source coordinates. Feed those four numbers into another node to apply the identical crop to a matching depth map or mask that shares the source geometry.

Where it shines

Pair it with a detector: an Impact Pack BBOX detector gives you a mask of the thing you care about (or SAM gives you a tight one), and instead of a full detailer pass you just want the region isolated. The README also positions it as the batch-cropping companion to Focal Rescale Rel - each image knows what to keep, and the node figures out where it is.

Gotchas

Here's the real one, straight from the source: this node is for single images. The multi-image path in this pack's crop nodes is not trustworthy - feed one image and mask pair at a time. And watch force_square: for a wide subject, a forced square crop throws away a lot of context. If the subject's shape matters, turn it off.

Installing it

Zero extra dependencies - pure PIL/numpy/torch, already in ComfyUI. ComfyUI Manager: search for Comfy_Felsirnodes (or "Felsir"), install, restart. Manual: cd ComfyUI/custom_nodes and git clone https://github.com/Felsir/Comfy_Felsirnodes, restart. It's under Add Node → Felsir.

CategoryFelsir

Inputs (6)

NameTypeDefaultDescription
image_inIMAGE
mask_inMASK
resolutionINT51216–2048
fit_insideBOOLEANfalse
force_squareBOOLEANtrue
paddingINT0

Outputs (5)

NameTypeDescription
IMAGEIMAGE
rect_leftINT
rect_topINT
rect_widthINT
rect_heightINT