LayerUtility: CropByMask V4
Smart crop that knows what the mask means
- image
- mask_image
- crop_box
- cropped_image
- cropped_mask
- crop_box
- box_preview
BoundedImageCropWithMask gives you a tight rectangle around a mask. CropByMask V4 is the smarter, more opinionated version: it gives you three different ideas of what "around the mask" should mean, lets you add reserve margins and round the crop to a friendly multiple, and - the real feature - it can reuse one computed crop box across an entire video, so every frame gets cropped identically instead of the box jittering as the mask shifts.
This is a port of the LayerStyle/RunningHub RunningHub/LayerUtility node, and V4's two additions over earlier versions are exactly those: reuse_crop_box (compute once, cache, reuse) and a device choice for GPU-accelerated mask blur. It pairs naturally with the pack's RestoreCropBox V2, which pastes the result back using the same crop_box.
The three detect modes
mask_area- the plain bounding box of the mask's non-zero region. What you'd expect.min_bounding_rect- the minimum bounding rectangle: the smallest rotated-into-place rectangle that contains the mask. If your mask is a diagonal person or a tilted object, this hugs it much tighter than an axis-aligned box.max_inscribed_rect- the largest rectangle that fits entirely inside the mask. Good for cropping to a clean interior region when the mask is an irregular blob and you want no background leaking in.
Inputs and outputs
Required: detect (the mode above), top/bottom/left/right_reserve (margins to add, default 20, negatives allowed), round_to_multiple (8–512 or None), reuse_crop_box, device. Optional: image, mask_image, and crop_box (if you already have a box, feed it and it's used directly).
Outputs: cropped_image, cropped_mask, crop_box (the BOX for restore/other nodes), and box_preview - an image with the detected region drawn in red and the final crop box in green. That preview output is worth wiring to a Preview node: it's the fastest way to see what the node thinks it's cropping before you trust it on a long batch.
How it works
It blurs the mask (Gaussian, radius 20) to smooth noise, runs the chosen detection, adds the reserve margins, rounds the width/height up to your multiple (centering the extra), and caches the result when reuse_crop_box is on. The first frame computes; every subsequent frame reuses the cached box. It also validates its inputs with clear errors - no mask, no box, no cache, and it tells you.
Installing it
It ships in ComfyUI_Swwan:
cd ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan
pip install -r ComfyUI_Swwan/requirements.txt
Or via ComfyUI Manager, search "ComfyUI_Swwan".
Where people get burned
reuse_crop_box is a class-level cache, so if you've got two instances of this node in one workflow, they share the cached box - and a change in one can silently affect the other. If the crop suddenly jumps for no reason, that's the culprit. And the cache persists for the process, so after you tweak a mask, clear the cache (reload the workflow or restart) before trusting the reuse path. The round_to_multiple centering can also push the box past the canvas edge on small images; the node clamps, but tight crops can end up off-center - check the box_preview before you commit.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| detect | COMBO | 3 options: mask_area, min_bounding_rect, max_inscribed_rect | |
| top_reserve | INT | 20-9999–9999 | — |
| bottom_reserve | INT | 20-9999–9999 | — |
| left_reserve | INT | 20-9999–9999 | — |
| right_reserve | INT | 20-9999–9999 | — |
| round_to_multiple | COMBO | 8 options: 8, 16, 32, 64, 128, 256, +2 | |
| reuse_crop_box | BOOLEAN | false | — |
| device | COMBO | CPU | 2 options: CPU, GPU |
| imageopt | IMAGE | — | |
| mask_imageopt | IMAGE | — | |
| crop_boxopt | BOX | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| cropped_image | IMAGE | — |
| cropped_mask | IMAGE | — |
| crop_box | BOX | — |
| box_preview | IMAGE | — |