Nodes/ComfyUI_Swwan/CropByMask V2 (Swwan) · 旧版兼容
ComfyUI Node

CropByMask V2 (Swwan) · 旧版兼容

The legacy crop that still hands you a BOX

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
CropByMask V2 (Swwan) · 旧版兼容
  • image
  • mask
  • crop_box
  • croped_image
  • croped_mask
  • crop_box
  • box_preview
◄invert_maskfalse►
◄detect▾►
◄top_reserve20►
◄bottom_reserve20►
◄left_reserve20►
◄right_reserve20►
◄round_to_multiple▾►

What it is, and why it's still here

This is 孤海's LayerStyle CropByMask V2, alive in the Swwan pack under a new node ID. It crops an image and its mask down to the bounding region of that mask, with margins, snapped to a multiple of your choosing - then hands you the crop, the mask, the box, and a preview with the box drawn on it.

That box is the reason people still load this node. In a detailer or regional-edit workflow the crop is only half the job; the other half is knowing where to paste the result back, and crop_box is that coordinate. The node even says which one to use instead: CropByMask V5 (Swwan) is the recommended entry, and this one exists to keep old graphs behaving. Same for the V3 and V4 variants in the same menu group. The old IDs (LayerUtility: CropByMask V2) are not registered as aliases - the display name looking familiar does not mean your old workflow will just load.

How it decides the box

If you don't feed it a crop_box, it detects one. The mask is Gaussian-blurred (radius 20) and then measured according to detect:

  • mask_area - the plain bounding area of the mask.
  • min_bounding_rect - the smallest rotated rectangle that contains it.
  • max_inscribed_rect - the largest rectangle that fits inside it.

That box then gets pushed out (or pulled in) by top_reserve, bottom_reserve, left_reserve, right_reserve - all default 20, and all allowing negative values, which shrink the box instead of growing it. Then round_to_multiple rounds the crop up to 8, 16, 32, 64, 128, 256, 512, or None, re-centring the box and clamping it back inside the canvas so you don't ask for pixels that don't exist. SD/SDXL latents want multiples of 8 and video models usually want more, which is the whole point of that dropdown.

Inputs and outputs

Required: image, mask, invert_mask (default off), detect, the four reserves, round_to_multiple. Optional: crop_box - feed a BOX in and the detection step is skipped entirely, so you can apply one geometry to a second image.

Outputs, in order: croped_image (yes, the author's spelling, preserved from the original), croped_mask as a real MASK, crop_box as a BOX, and box_preview - the source image with the detected region in red and the final crop in green. The preview is genuinely useful the first time you tune the reserves: you can see which way the box moved.

One mask rule to remember: if the incoming MASK has more than one frame, V2 uses only the first and prints a warning to the console. Silent-ish, and easy to misread as "the node ignores my mask".

Install

cd ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan
cd ComfyUI_Swwan
python -m pip install -r requirements.txt

Or Manager → ComfyUI Swwan. Restart, hard-refresh. This node is pure PIL/torch - no models, no extra pip packages beyond the base list (numpy, Pillow, opencv-python, scipy, scikit-image).

Gotchas

BOX is not BBOX, and neither is IMAGE_BOUNDS. The pack ships several of these geometry types, each with its own protocol, and they do not interchange. V2's crop_box goes to a restore node from the same family, not into a random core node expecting a bounding box.

The legacy/duplicate-name warning. If ComfyUI Manager tells you it found a conflict with ComfyUI_Swwan, that's about overlapping node names across packs - historically this pack's territory overlapped KJNodes' and LayerStyle's. It's a warning, not a fault. In practice on the current 1.0.0 source this node registers under its own Swwan ID and no longer claims the LayerStyle aliases, so the real symptom of old workflows is a red missing-node box, not a crash.

Detection is heuristic. Blur-then-measure on a mask with stray speckles will happily include the speckles. Clean the mask upstream (the pack's Mask Process does this) if the box looks wrong, and use the preview to confirm before you trust round_to_multiple numbers.

Multiple images in, one mask in. The crop applies to every frame of the input batch using a single box. If your frames move, you get cropping that's only right for the mask you fed it - that's the moment to look at V5 or a per-frame loop instead.

CategorySwwan/Legacy

Inputs (10)

NameTypeDefaultDescription
imageIMAGE—
maskMASK—
invert_maskBOOLEANfalse—
detectCOMBO3 options: mask_area, min_bounding_rect, max_inscribed_rect
top_reserveINT20-9999–9999—
bottom_reserveINT20-9999–9999—
left_reserveINT20-9999–9999—
right_reserveINT20-9999–9999—
round_to_multipleCOMBO8 options: 8, 16, 32, 64, 128, 256, +2
crop_boxoptBOX—

Outputs (4)

NameTypeDescription
croped_imageIMAGE—
croped_maskMASK—
crop_boxBOX—
box_previewIMAGE—