tri3d-image-mask-2-box
Crop an image to exactly the box your mask defines
- image
- mask
- IMAGE
Every mask implies a bounding box - the smallest rectangle that contains all its white pixels. tri3d-image-mask-2-box is the node that acts on that thought: give it an image and a mask, and it crops the image down to the mask's bounding box. It's the crop half of a crop/stitch pair, and it's the kind of tiny utility that saves you from writing the same ten lines of OpenCV logic in a custom script for the hundredth time.
What it does
Two inputs: image (an IMAGE) and mask (a MASK). It computes the bounding box of the mask - the min/max rows and columns that contain any mask pixels - and returns the image cropped to exactly that rectangle, at full resolution, no margin, no resize. If the mask is empty, it falls back to the full image rather than erroring.
The output is a single IMAGE. What you do with it is up to you - it's typically the "extract the subject for closer processing" move: crop the head, crop a garment, crop the person, then run the crop through an upscaler, an inpaint, or a ControlNet pass.
Two things worth knowing:
- The companion node is
tri3d-image-mask-box-2-image("Stitch box to image"), which takes the cropped box plus the original image and mask and pastes the crop back into place. The pair is designed to be used as a round-trip: crop with this, process the crop, stitch back with the other, and the geometry lines up because both derive the box from the same mask. - No margin option. Unlike
tri3d-extract-parts-batch, there's no padding control here - you get the tight box or you pad the mask yourself first (dilate it) to get a roomier crop.
Where it fits
In the pack's VTON flows, masks are everywhere - from tri3d-extract-masks-batch, from background removal, from the clean_mask filter - and cropping to a mask's box is the recurring follow-up. It's also useful in a pinch for "crop to this specific region" workflows where you'd otherwise hand-draw a box.
Installing
ComfyUI Manager (search "tri3d"), or:
cd ComfyUI/custom_nodes
git clone https://github.com/TRI3D-LC/tri3d-comfyui-nodes
Restart, install requirements (pip install -r custom_nodes/tri3d-comfyui-nodes/requirements.txt). No downloads.
Troubleshooting
- Crop is too tight - no margin by design. Dilate the mask, or use the parts-batch node with its
marginif this is a segmentation crop. - Whole image comes back - the mask was empty, so the bounding box fell back to the full frame. Check your mask actually has white pixels.
- Position lost downstream - that's what the stitch-back node is for; crop alone can't remember where it came from.
Tiny, obvious, and exactly the kind of thing you shouldn't have to write yourself. Pair it with the stitch node and you've got a clean crop-process-restore loop.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |