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

LayerUtility: CropByMask V2

CropByMask V2

By chflame163·Created 3 years ago·Updated 4 days ago· 3,113
LayerUtility: CropByMask V2
  • 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 is the "zoom in on the subject, work at full detail, paste it back" node, and it's a backbone of serious ComfyUI workflows. The problem it solves: your subject is a small region of a big image, and running a detailer or inpaint on the whole frame wastes resolution and compute on empty background. So you crop tight to the mask, do your high-quality work on just that patch, and composite it back using the crop coordinates this node hands you. Face detailing, hand fixing, object inpainting - they all lean on this pattern.

The mechanism is bounding-box detection from a mask. You give it an image and a mask, and it finds the region the mask occupies, crops both to that region (plus padding), and returns the crop along with a crop_box describing where it came from - so a later paste-back node knows exactly where to put the result. It's the crop half of a crop/paste round trip.

The controls you'll actually set: detect chooses how the box is computed - mask_area follows the mask's extent, min_bounding_rect is the tightest rectangle around it, max_inscribed_rect fits inside it. mask_area is the sane default. The four *_reserve values (top, bottom, left, right, default 20) add padding around the detected region so you don't crop right up against the subject's edge - give inpainting some context to blend into. round_to_multiple snaps the crop dimensions to 8, 16, 32… up to 512 (or None), which matters because most samplers and models want dimensions divisible by 8 or 64; set this and you won't get resize errors downstream. invert_mask flips which side is the subject. There's an optional crop_box input if you want to force a specific region instead of detecting one.

The outputs are the crop/paste toolkit: croped_image and croped_mask (the cropped patch and its mask - note the pack's spelling of "croped"), crop_box (the BOX you feed to a paste-back node to composite the result home), and box_preview, a visualization of where the crop landed on the original so you can sanity-check it at a glance.

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

One note worth acting on: there's a V3 of this node in the pack, and it's the newer iteration - if you're building a fresh workflow, prefer CropByMask V3 unless something you're copying specifically wired V2. Functionally they cover the same job. Beyond that, the two things that bite are forgetting round_to_multiple (leave it off and your crop can land on odd dimensions that break the next node) and skimping on reserve (crop too tight and inpainting has no surrounding context to blend into, so the patch looks stitched-in). And the standing LayerStyle caveat: if the node won't appear, the pack failed to import on a dependency conflict - check the console, "Try Fix" in Manager, reinstall requirements.

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—