Nodes/ComfyUI Layer Style/LayerUtility: CropByMask V3
ComfyUI Node Runs on cloud

LayerUtility: CropByMask V3

CropByMask V3

By chflame163·Created 3 years ago·Updated 5 days ago· 3,113
LayerUtility: CropByMask V3
  • 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▾►

CropByMask V3 is the current version of the pack's crop-to-subject node, and it's one you'll wire into almost any serious detailing or inpainting workflow. The core idea: your subject is often a small part of a large image, and running a detailer or a sampler over the whole frame throws resolution and compute at empty background. So you crop tight to the mask, do your high-quality work on that patch alone, and paste it back using the crop coordinates this node returns. That crop/paste round trip is how people get sharp faces and clean hands without upscaling the entire canvas.

The mechanism is mask-driven bounding-box detection. Feed it an image and a mask; it locates the region the mask covers, crops both to it (plus padding), and hands back a crop_box recording exactly where the patch came from, so a paste-back node can drop your edited result into the right spot in the original.

The settings that matter: detect picks the box-finding method - mask_area tracks the mask extent, min_bounding_rect gives the tightest enclosing rectangle, max_inscribed_rect fits inside the mask. mask_area is the default and usually right. The four *_reserve values (top/bottom/left/right, default 20) pad the crop so the subject isn't jammed against the edge - inpainting needs surrounding context to blend into, so don't set these to zero. round_to_multiple snaps crop dimensions to 8/16/32/…/512 or None; set it, because most models want dimensions divisible by 8 or 64 and this prevents the odd-size errors that otherwise bite you two nodes later. invert_mask flips the subject/background sense. An optional crop_box input lets you supply a fixed region instead of detecting one.

Outputs are the crop/paste kit: croped_image and croped_mask (the patch and its mask - that's the pack's spelling), crop_box (the BOX for the paste-back step), and box_preview, a quick visualization of where the crop landed on the full image so you can eyeball whether it caught the whole subject.

Install is the pack standard: in ComfyUI Manager search "ComfyUI Layer Style" and install, or cd ComfyUI/custom_nodes && git clone https://github.com/chflame163/ComfyUI_LayerStyle, then pip install -r requirements.txt and restart. No models to download.

Since this is V3, it's the one to use for new workflows - earlier V2 (and the original) do the same job, and V3 is the refined successor, so there's no reason to start on an older one. The recurring mistakes are the same across versions: leave round_to_multiple off and your crop can come out at dimensions that break the next node; set the reserves too tight and the inpaint has no context to blend into, so the patch looks stitched on. Give it a few dozen pixels of reserve and snap to a multiple, and the paste-back is invisible. And the ever-present LayerStyle warning: if the node doesn't appear after install, the pack failed to import on a Python dependency conflict - check your console for the traceback, run "Try Fix" in Manager, and reinstall the pack's requirements into your venv.

Category😺dzNodes/LayerUtility

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—