Nodes/ComfyUI-Light-Tool/Light-Tool: Mask Bounding Box Cropping
ComfyUI Node

Light-Tool: Mask Bounding Box Cropping

Auto-crop the empty border off a mask in one node

By ihmily·Created 2 years ago·Updated 4 months ago· 20
Light-Tool: Mask Bounding Box Cropping
  • image
  • image

Upscaled, letterboxed, or padded - mask images accumulate dead space around the actual subject, and dead space wrecks inpainting crops and throws off placement math. Light-Tool: MaskBoundingBoxCropping finds the tightest rectangle that contains the mask and crops to it. One image in, one tightly-cropped image out.

One input: image. One output: image. No knobs, no parameters - which is either refreshing or frustrating depending on how you feel about nodes that refuse to let you tune them.

How it works. The image is converted to grayscale, then the gray values are used as an alpha channel: bright pixels are treated as opaque, dark pixels as transparent. OpenCV's boundingRect finds the smallest rectangle around the non-empty pixels, and the image is cropped to it. Then a genuinely interesting detail: the crop is composited onto a solid black background. So the output is the subject cropped tight, sitting on black, rather than left with a transparent surround.

The implications. Because it's black-composited, the output isn't a floating cutout - it's the mask region on black, which is exactly what you want for inpainting (black reads as "no mask" in most inpainting pipelines) or for feeding a cropped region into a detailer before re-placing it. But if you were hoping for a transparent-cropped PNG, that's the pack's BoundingBoxCropping job, not this one. The black background is a deliberate choice and it changes how the output behaves downstream.

The traps. The node relies on the alpha it builds from brightness, so a mask that's mostly mid-gray instead of white can produce a sloppy bounding box. And because it crops based on any non-empty pixel, a stray white speck in a corner of the mask will blow the crop wide open - clean up salt-and-pepper noise with the pack's MorphologicalTF (erode a pass) before cropping if you see that. Also note the output is RGBA on black; if your downstream node expects plain RGB you may need a conversion.

When you'd reach for it. Auto-trimming masks before an inpainting pass, standardizing crop regions for batch face/region detailers, or any time a mask-generating model (SAM, BiRefNet-style segmentation) returns a mask padded to canvas size and you want the subject's bounding box as a tight crop. The fact that it returns the cropped region rather than coordinates is a small limitation - if you need the bbox values themselves (x, y, w, h) to feed a crop-and-paste loop, you'll need a different node.

Installing. With the pack:

cd ComfyUI/custom_nodes
git clone https://github.com/ihmily/ComfyUI-Light-Tool.git
pip install -r requirements.txt
# restart ComfyUI

Or ComfyUI Manager → search ComfyUI-Light-Tool → Install. No models; opencv-python does the bounding-box work and is in the pack's requirements.

Simple, effective, and worth remembering that its output is "crop on black," not "floating cutout." Once you've got that convention straight, it's a one-node fix for a chore you'd otherwise script by hand.

CategoryComfyUI-Light-Tool/image/ImageCrop

Inputs (1)

NameTypeDefaultDescription
imageIMAGE

Outputs (1)

NameTypeDescription
imageIMAGE