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

LayerUtility: ImageAutoCrop(Advance)

ImageAutoCrop — find the subject, crop to a target ratio, in one node

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

This is the original ImageAutoCrop - the version that finds your subject for you, rather than requiring an existing mask. Per the README, the whole family exists "to generate the image materials for training models": feed it a batch of raw photos, get back consistently-cropped, consistently-ratioed subject images, without hand-masking each one. It does that by running its own subject detection internally, then cropping around whatever it finds.

How it works

matting_method picks how the subject gets separated from the background: RMBG 1.4, a fast general-purpose background-removal model that needs no prompt, or SegmentAnything, which uses GroundingDINO to find a region matching your sam_prompt text (default "subject") and SAM to turn that into a precise mask. The README is direct about the tradeoff - "RMBG 1.4 runs faster" - and it's also the lighter install, since it skips the SAM/GroundingDINO model downloads entirely. Reach for SegmentAnything specifically when you need to target something described by text rather than "the obvious foreground subject," which is all RMBG can do.

Once it has a mask, detect decides how the crop boundary is drawn around it (min_bounding_rect, max_inscribed_rect, or mask_area), and aspect_ratio forces the final crop to a fixed shape - pick from common ratios, custom (paired with proportional_width/proportional_height), or detect_mask to let the detected mask's own shape drive the ratio instead of forcing one.

The inputs and outputs that matter

  • image - your source.
  • matting_method / sam_prompt / sam_threshold - as above; sam_prompt and sam_threshold only matter when matting_method is SegmentAnything.
  • aspect_ratio, proportional_width, proportional_height - target shape.
  • scale_to_longest_side (bool) + longest_side - resize the result so its longest edge hits this pixel value.
  • border_reserve (default 100, range −9999 to 9999) - per the README, "expand the cutting range beyond the detected mask body area." A positive value gives the subject more breathing room in the crop; the schema allows negative values too, which would pull the crop in tighter than the detected bounds.
  • ultra_detail_range (0–256, default 0) - extra fine-edge processing on the mask; leave at 0 to save time unless you're seeing rough edges specifically at hair or fur.
  • background_color - fills whatever's outside the subject in the final crop.
  • sam_model / grounding_dino_model - which SAM/GroundingDINO weight to use, only relevant on the SegmentAnything path.

Outputs: cropped_image (the result), box_preview (a visual of where the crop landed - check this first if a batch looks wrong), cropped_mask (the detected mask).

How to install it

Recommended: ComfyUI Manager, search "ComfyUI Layer Style Advance". Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/chflame163/ComfyUI_LayerStyle_Advance.git

Then install_requirements.bat / install_requirements_aki.bat, or pip install -r requirements.txt plus repair_dependency.bat. Restart.

Model files depend entirely on which matting_method you'll use. For RMBG 1.4, that model's weights are what's needed - no SAM download. For SegmentAnything, you're signing up for a heavier stack: bert-base-uncased files into ComfyUI/models/bert-base-uncased, GroundingDINO config + weights (694MB–938MB) into ComfyUI/models/grounding-dino, and a SAM model of your choice (from a 39MB mobile_sam up to a 2.57GB sam_hq_vit_h) into ComfyUI/models/sams. If all you need is "cut out the obvious subject," RMBG 1.4 avoids that whole download.

Common issues & troubleshooting

Crop is off, or grabs the wrong thing. Check box_preview first - it's a direct visual of what the node actually detected before you assume a parameter is wrong. If you're on SegmentAnything, a vague or mismatched sam_prompt is the most common cause; make it more specific to what's actually in frame.

SegmentAnything path is much slower than RMBG. Expected - it's running two models (GroundingDINO + SAM) instead of one, and larger SAM checkpoints (the _h variants) are noticeably heavier than mobile_sam. If speed matters more than precision, drop to a smaller SAM checkpoint or switch to RMBG entirely.

Cannot import name 'guidedFilter' from 'cv2.ximgproc' or NameError: name 'guidedFilter' is not defined. A known pack-wide dependency issue caused by an incorrect opencv-contrib-python version, or that package being overwritten by a different opencv install. repair_dependency.bat (or the Aki equivalent) is the documented fix.

Cannot import name 'VitMatteImageProcessor' from 'transformers'. Low transformers version - same fix, run the repair script.

Category😺dzNodes/LayerUtility

Inputs (15)

NameTypeDefaultDescription
imageIMAGE
background_colorSTRING#FFFFFF
aspect_ratioCOMBO9 options: 1:1, 3:2, 4:3, 16:9, 2:3, 3:4, +3
proportional_widthINT21–999
proportional_heightINT11–999
scale_to_longest_sideBOOLEANtrue
longest_sideINT10244–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

Outputs (3)

NameTypeDescription
cropped_imageIMAGE
box_previewIMAGE
cropped_maskMASK