AP Mask Trim
Cut a mask down to size — and trim along the shape's own axis
- mask
- mask
Your mask covers the whole head, but you only wanted the hair. SAM handed you a body silhouette and you need just the top third. That's the situation AP Mask Trim exists for: it trims any MASK by percentage along the X or Y axis with a feathered edge, without resizing anything. The output canvas keeps the same dimensions - trimmed pixels just fade to black. It's a small utility, but once you need it, you need it.
It ships in the ◧ AP ClipSEG Light pack, and the README's example is the most natural use: chain a text-prompted mask (prompt=hair) into this node with crop_y=-50 and you've isolated the top of the head. You're not limited to CLIPSeg masks though - any MASK works, including output from SAM, BiRefNet, or a manual paint node. If you've got a region that's right but too big, this is the cleanest way to slice it.
How it works
Mechanically it's simpler than it looks: the mask gets multiplied by a per-axis weight ramp, so trimmed pixels fade to zero over the feather zone instead of being hard-clipped. No image rotation, no cropping - just a fade.
- crop_x - positive trims from the right, negative from the left.
30removes the rightmost 30%. - crop_y - positive trims from the bottom, negative from the top.
-40removes the top 40%. - feather (default 10) - the soft-edge width in pixels at the cut boundary. 0 is a hard cut; anything above gives a gradual fade so downstream inpaint doesn't show a seam.
- auto_align - the interesting one. When
True, the node computes the principal orientation of the masked shape (weighted PCA) and rotates the trim axes to match.crop_ythen always trims the "bottom" of the shape, even if it's a tilted head or a diagonal limb in the frame. Flip this on for anything rotated. - mask_dilate / mask_blur - expand the trimmed mask or soften the final edges.
Output is a single mask (MASK), same shape as the input, so it drops straight into whatever consumed the original.
One subtle gotcha
Watch what the percentage means. In plain mode, crop_x is a percentage of the canvas width, not of the mask's extent - so if your mask only covers a small corner of a large frame, 50 slices half the frame, not half the object. In auto_align mode the math switches to a percentage of the masked region's own extent, which is usually what you actually wanted. If a trim feels too aggressive, that's the distinction biting you, not a bug.
Installing
There's no separate install - this node comes with the pack. If you don't have it yet:
cd ComfyUI/custom_nodes
git clone https://github.com/adampolczynski/ComfyUI_AP_ClipSEG_Light
or search "AP ClipSEG Light" in ComfyUI Manager and restart. It's pure PyTorch - no extra model downloads, no added dependencies beyond the pack's torch requirement.
Bottom line
It's the rare "tiny tool that just works" node: pure torch, nothing to download, nothing to break, and a genuinely clever auto_align mode that solves the tilted-head problem most trim nodes ignore. Keep it next to your CLIPSeg Text Mask and you've got a fast prompt-to-region pipeline that never touches a detector model. If your masks are always already the right size, you'll never need it - but the day one isn't, it's the difference between a five-minute trim and rebuilding the workflow.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| crop_x | INT | 0-100–100 | Trim % from right (positive) or left (negative). 0 = no trim. |
| crop_y | INT | 0-100–100 | Trim % from bottom (positive) or top (negative). 0 = no trim. |
| auto_align | BOOLEAN | false | When True, trim axes follow the mask's principal orientation (PCA). crop_y trims the 'bottom' of the masked shape even when it is rotated. |
| feather | INT | 100–200 | Soft-edge width in pixels at the cut boundary. |
| mask_dilate | INT | 00–64 | Dilate (expand) the trimmed mask by this many pixels. |
| mask_blur | INT | 00–64 | Gaussian blur applied to the final mask. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |