LayerUtility: ImageAutoCrop V2(Advance)
ImageAutoCrop V2 — ComfyUI Node Guide
- image
- mask
- cropped_image
- box_preview
- cropped_mask
This is the "find the subject, crop tight, output at a specific size and aspect ratio" node, and the README is refreshingly honest about why it exists: it's built to generate training-image material for models. If you've ever manually cropped a folder of dataset images to a consistent square or portrait ratio, this is the node that automates that job - with a built-in matting step so it can find the subject on its own if you don't hand it a mask.
The mechanism has two halves. First, it needs to know what the subject is: either you feed it a mask directly (the optional input), or it detects one itself using matting_method, which offers RMBG 1.4 (faster) or SegmentAnything (SAM + GroundingDINO, generally more accurate on odd subjects). Second, once it has a mask, it figures out the crop box, resizes to your target, and fills whatever's left over with background_color.
The inputs that actually decide the output:
aspect_ratio- pick from common ratios (1:1, 3:2, 4:3, 16:9, 2:3, 3:4, 9:16),custom(paired withproportional_width/proportional_height), ordetect_mask/originalto just follow the source.scale_to_side+scale_to_length- once cropped, scale the result by longest edge, shortest edge, width, or height to hit a specific pixel size.matting_method,sam_model,grounding_dino_model,sam_threshold,sam_prompt- only relevant if you're not feeding in your own mask; these control how SAM finds your subject whenmatting_methodisSegmentAnything.sam_promptdefaults to"subject", which is intentionally generic - narrow it if SAM keeps grabbing the wrong thing.
Two more worth knowing: fill_background (default true) decides whether the space outside the crop gets filled with background_color, or left untouched if false - that's new in V2 versus the original ImageAutoCrop. And detect picks how the bounding box gets calculated from the mask: min_bounding_rect (tightest fit), max_inscribed_rect (the largest rectangle that stays fully inside the mask), or mask_area. border_reserve pads the crop out from the detected region if you don't want an edge-to-edge crop, and ultra_detail_range/process_detail-adjacent settings on the matting side control how much time gets spent refining mask edges - 0 skips that pass entirely for speed.
Outputs: cropped_image (the final result), box_preview (a visual of where it decided to crop, handy for sanity-checking a batch), and cropped_mask (the mask that matches the crop, useful if you're feeding this into further compositing).
Installing it: ComfyUI Manager search for "ComfyUI Layer Style Advance," or git clone https://github.com/chflame163/ComfyUI_LayerStyle_Advance into custom_nodes, then install_requirements.bat / pip install -r requirements.txt, then repair_dependency.bat, restart. If you're using SegmentAnything as your matting method, you'll also need the SAM and GroundingDINO model files - the README points to the same install path as the pack's SegmentAnythingUltra nodes (config/model files for bert-base-uncased, GroundingDINO, and your chosen SAM variant, sized from ~40MB up to ~2.5GB depending which one you pick).
Common trip-ups: if the crop keeps missing your subject, check sam_prompt first - a bare "subject" can grab the wrong region on a busy image, and getting more specific usually fixes it faster than tweaking thresholds. And if edges look rough on a fast preview, remember ultra_detail_range at 0 is deliberately skipping edge refinement to save time - bump it up when you need the final output, not just a quick check.
Inputs (17)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| fill_background | BOOLEAN | true | — |
| background_color | STRING | #FFFFFF | — |
| aspect_ratio | COMBO | 10 options: 1:1, 3:2, 4:3, 16:9, 2:3, 3:4, +4 | |
| proportional_width | INT | 11–999 | — |
| proportional_height | INT | 11–999 | — |
| scale_to_side | COMBO | 5 options: None, longest, shortest, width, height | |
| scale_to_length | INT | 10244–999999 | — |
| detect | COMBO | 3 options: min_bounding_rect, max_inscribed_rect, mask_area | |
| border_reserve | INT | 100-9999–9999 | — |
| ultra_detail_range | INT | 00–256 | — |
| matting_method | COMBO | 2 options: RMBG 1.4, SegmentAnything | |
| sam_model | COMBO | 7 options: sam_vit_h (2.56GB), sam_vit_l (1.25GB), sam_vit_b (375MB), sam_hq_vit_h (2.57GB), sam_hq_vit_l (1.25GB), sam_hq_vit_b (379MB), +1 | |
| grounding_dino_model | COMBO | 2 options: GroundingDINO_SwinT_OGC (694MB), GroundingDINO_SwinB (938MB) | |
| sam_threshold | FLOAT | 0.300–1 | — |
| sam_prompt | STRING | subject | — |
| maskopt | MASK | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| cropped_image | IMAGE | — |
| box_preview | IMAGE | — |
| cropped_mask | MASK | — |