Nodes/ComfyUI_LayerStyle_Advance/LayerUtility: ImageAutoCrop V2(Advance)
ComfyUI Node Runs on cloud

LayerUtility: ImageAutoCrop V2(Advance)

ImageAutoCrop V2 — ComfyUI Node Guide

By chflame163·Created 2 years ago·Updated 4 months ago· 696
LayerUtility: ImageAutoCrop V2(Advance)
  • image
  • mask
  • cropped_image
  • box_preview
  • cropped_mask
fill_backgroundtrue
background_color#FFFFFF
aspect_ratio
proportional_width1
proportional_height1
scale_to_side
scale_to_length1024
detect
border_reserve100
ultra_detail_range0
matting_method
sam_model
grounding_dino_model
sam_threshold0.30
sam_promptsubject

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 with proportional_width/proportional_height), or detect_mask/original to 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 when matting_method is SegmentAnything. sam_prompt defaults 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.

Category😺dzNodes/LayerUtility

Inputs (17)

NameTypeDefaultDescription
imageIMAGE
fill_backgroundBOOLEANtrue
background_colorSTRING#FFFFFF
aspect_ratioCOMBO10 options: 1:1, 3:2, 4:3, 16:9, 2:3, 3:4, +4
proportional_widthINT11–999
proportional_heightINT11–999
scale_to_sideCOMBO5 options: None, longest, shortest, width, height
scale_to_lengthINT10244–999999
detectCOMBO3 options: min_bounding_rect, max_inscribed_rect, mask_area
border_reserveINT100-9999–9999
ultra_detail_rangeINT00–256
matting_methodCOMBO2 options: RMBG 1.4, SegmentAnything
sam_modelCOMBO7 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_modelCOMBO2 options: GroundingDINO_SwinT_OGC (694MB), GroundingDINO_SwinB (938MB)
sam_thresholdFLOAT0.300–1
sam_promptSTRINGsubject
maskoptMASK

Outputs (3)

NameTypeDescription
cropped_imageIMAGE
box_previewIMAGE
cropped_maskMASK