Cut by mask aspect ratio v5.1.0
Crop to your mask, fit the aspect ratio, and pad the sides to keep it centered
- image
- mask
- IMAGE
Mask-crop nodes usually stop at the bounding box. tri3d_CutByMaskAspectRatio goes further: it crops to the mask, then forces the result into the target width/height ratio you specify - extending and padding with a solid color when the subject is too narrow, cropping evenly from the sides when it's too wide, and resizing to your target dimensions at the end. It's a "make this subject fit exactly in a 512×512 box" node, built for feeding crops into fixed-size models without squashing the subject.
What it does
Five inputs, two of which have a surprise:
image- the photo.mask- note the type: IMAGE, not MASK. It's a mask-as-image (white on black works); the node thresholds it at 127 and finds the contours.margin- padding around the mask box, default 10.target_width/target_height- the output size, default 512×512.padding_color- the fill color for the padded areas, 0–255, default 255 (white).
The logic: bounding box of the mask, plus margin, then compare the box's aspect ratio to the target. If the box is too narrow, it extends the crop sideways and fills the extra space with padding_color (useful on a white or transparent-feel background); if too wide, it crops evenly from both sides; if it already matches, it just crops. Either way it finishes by resizing to target_width × target_height with Lanczos interpolation.
The one output is the final IMAGE at exactly your target size, subject kept centered and undistorted - nothing gets stretched to fit.
Where it fits
Any pipeline that needs uniformly-sized, aspect-correct crops: training sets, batch processing for a model that wants square inputs, or the pack's garment work where a cropped part has to land in a fixed canvas. Because the mask input is an IMAGE, it plays nicely with segmentation maps and masks you've already visualized, not just raw MASK tensors.
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 model downloads - OpenCV geometry and resize.
Troubleshooting
- Black bars where you wanted transparency -
padding_colorfills with a solid value; 255 gives white. If your downstream expects alpha, you'll need to add it yourself. - Subject squished or distorted - it shouldn't be; the whole point is aspect-ratio preservation. If it looks wrong, check that your
maskis actually binary and tight around the subject - a loose mask makes a loose box. - "No contours found" console message - the node falls back to returning the original image when the mask is empty, which can quietly bypass your target size.
A crop node with opinions about aspect ratios. For fixed-size downstream models it's usually the difference between "the model stretched my subject" and "the subject arrived centered and correct."
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | IMAGE | — | |
| margin | INT | 100–100 | — |
| target_width | INT | 51264–4096 | — |
| target_height | INT | 51264–4096 | — |
| padding_color | INT | 2550–255 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |