ComfyUI Node

AP Mask Trim

Cut a mask down to size — and trim along the shape's own axis

By adampolczynski·Created 5 months ago·Updated 5 months ago· 1
AP Mask Trim
  • mask
  • mask
crop_x0
crop_y0
auto_alignfalse
feather10
mask_dilate0
mask_blur0

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. 30 removes the rightmost 30%.
  • crop_y - positive trims from the bottom, negative from the top. -40 removes 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_y then 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.

CategoryAP ClipSEG Light

Inputs (7)

NameTypeDefaultDescription
maskMASK
crop_xINT0-100–100Trim % from right (positive) or left (negative). 0 = no trim.
crop_yINT0-100–100Trim % from bottom (positive) or top (negative). 0 = no trim.
auto_alignBOOLEANfalseWhen True, trim axes follow the mask's principal orientation (PCA). crop_y trims the 'bottom' of the masked shape even when it is rotated.
featherINT100–200Soft-edge width in pixels at the cut boundary.
mask_dilateINT00–64Dilate (expand) the trimmed mask by this many pixels.
mask_blurINT00–64Gaussian blur applied to the final mask.

Outputs (1)

NameTypeDescription
maskMASK