LayerUtility: CropByMask
CropByMask — zoom into the subject, work at full detail, paste back
- image
- mask_for_crop
- croped_image
- croped_mask
- crop_box
- box_preview
The problem this solves is one of the more common inefficiencies in a ComfyUI graph: your subject is a small region of a much bigger image, and running a detailer or inpaint pass on the whole frame wastes resolution and compute on background that doesn't need touching. CropByMask crops tight to the mask, so whatever runs next works on just that patch at full detail - then hands you back everything needed to paste the result home. Face detailing, hand fixing, object-specific inpainting: this crop-work-stitch pattern is the backbone of most of them.
How it works
You give it an image and a mask_for_crop. It finds the region the mask occupies, crops both the image and the mask to that region plus your padding, and returns a crop_box describing exactly where that region came from - so a paste-back node downstream (this pack's RestoreCropBox, for instance) knows precisely where to composite the result. It's the crop half of a crop/paste round trip; something else has to do the pasting.
The inputs and outputs that matter
detect- how the crop region is computed:min_bounding_rect(the tightest rectangle around the mask),max_inscribed_rect(the largest rectangle that fits inside the mask), ormask_area(follows the mask's actual extent).mask_areais the sane default for most subjects.top_reserve/bottom_reserve/left_reserve/right_reserve(default 20 each) - padding added around the detected region on each side. Crop too tight and an inpaint has no surrounding context to blend into; these exist so you don't have to.invert_mask(default false) - flips which side counts as the subject.image,mask_for_crop- the source image and the mask defining what to crop to.
Outputs: croped_image and croped_mask (the cropped patch and its mask - yes, "croped" is the pack's actual spelling), crop_box (feed this to a paste-back node), and box_preview - a visualization of where the crop landed on the original image, useful for sanity-checking the region before you burn time on a heavy pass over it.
How to install it
Ships with the ComfyUI Layer Style pack. ComfyUI Manager - search ComfyUI Layer Style, install, restart. Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/chflame163/ComfyUI_LayerStyle
pip install -r ComfyUI_LayerStyle/requirements.txt
then restart. Lives under 😺dzNodes → LayerUtility. No models needed.
Common issues & troubleshooting
Crop lands on odd, awkward dimensions and the next node in the chain errors. This version of the node has no dimension-rounding guard - it crops to exactly whatever the mask plus your reserve values produce. If a sampler or detailer downstream wants dimensions divisible by 8 or 64, you'll want to handle that yourself (an image-scale node with a "round to multiple" option) or switch to CropByMask V2/V3, later revisions in this pack that added a round_to_multiple setting for exactly this.
Pasted-back result looks stitched-in, with a visible seam. Almost always too little padding. Bump the *_reserve values up so the crop carries some real background context for the next step to blend into, rather than cropping right up against the subject's edge.
Crop is in the wrong place, or seems to grab the background instead of the subject. Check invert_mask - it defaults to false here, unlike most of this pack's other nodes, so if your mask's polarity is the more common "white equals subject" convention you may not need to touch it at all; but if it's inverted from that, flip it.
Node's missing from your palette entirely. Pack import failure - LayerStyle's long dependency chain means a broken transformers/TensorFlow pairing takes the whole pack, every node in it, down at once. Check the startup console or hit "Try Fix" in Manager.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask_for_crop | MASK | — | |
| invert_mask | BOOLEAN | false | — |
| detect | COMBO | 3 options: min_bounding_rect, max_inscribed_rect, mask_area | |
| top_reserve | INT | 20-9999–9999 | — |
| bottom_reserve | INT | 20-9999–9999 | — |
| left_reserve | INT | 20-9999–9999 | — |
| right_reserve | INT | 20-9999–9999 | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| croped_image | IMAGE | — |
| croped_mask | MASK | — |
| crop_box | BOX | — |
| box_preview | IMAGE | — |