Nodes/comfyui_gvf/Crop Image to Min/Max Aspect Ratio
ComfyUI Node

Crop Image to Min/Max Aspect Ratio

Only fix the frames that need it

By gvfarns·Created about a year ago·Updated 9 months ago· 0
Crop Image to Min/Max Aspect Ratio
  • images
  • image
  • width
  • height
min_aspect0.67
max_aspect1.50

Most of the time when you crop an image to an aspect ratio, you want it cropped, period. But sometimes you're feeding a batch of inputs that are mostly fine - a set of reference frames, a pile of source images for training - and you only want to touch the ones that fall outside an acceptable range. That's the whole idea behind CropToAspectRatioMinMax ("Crop Image to Min/Max Aspect Ratio", pack comfyui_gvf): crop only when the image's aspect ratio is out of bounds, and leave everything else alone.

Think of it as the tolerant sibling of CropToAspectRatio. The inputs are images (an IMAGE tensor, batch-friendly) plus two floats defining the band you'll accept: min_aspect (default 0.6667, min 0.1, max 10) and max_aspect (default 1.5, min 0, max 10). Same convention as the rest of the pack - aspect is width ÷ height, so values below 1 are portrait, above 1 are landscape. The node computes the image's aspect and if min_aspect <= aspect <= max_aspect, it returns the image untouched. Only when the image is outside the band does it crop: too wide (aspect > max_aspect) means the width gets trimmed to height × max_aspect; too tall means the height gets trimmed to width / min_aspect. Either way, center-cropped.

The practical upshot: with the defaults, a 3:2 landscape image (1.5) passes through, an 8K-ultra 21:9 (2.33) gets its sides trimmed to 1.5, and a 1:1 square passes through - but a 9:16 phone shot (0.5625) gets its top and bottom trimmed to 0.6667. You can widen the band to tolerate more, or shrink it to enforce a tight target. Since max_aspect bottoms out at 0, a lazy way to only enforce a lower bound is setting max_aspect high (like 10) - then only too-tall images get touched.

It outputs image, plus width and height ints. And here's the same gotcha as the other crop node in this pack, so it's worth repeating once: those width/height outputs report the original input dimensions, not the post-crop ones. The source captures w, h before slicing and returns them unchanged, so don't wire them into a latent-size node expecting the cropped size. If the image passed through untouched, they're obviously right; if it was cropped, they're the "before" values.

Install is the standard shared routine for this pack - no dependencies at all, so it's one of the safest custom nodes you'll ever add:

cd ComfyUI/custom_nodes
git clone https://github.com/gvfarns/comfyui_gvf

or search "comfyui_gvf" in ComfyUI Manager, then restart. Where this node earns its keep: prepping mixed-aspect image batches before a ControlNet pass or video generation where out-of-range frames cause the worst artifacts, and cleaning up a dataset for training without wasting time re-cropping images that were already fine.

Categorygvf

Inputs (3)

NameTypeDefaultDescription
imagesIMAGE
min_aspectFLOAT0.670.1–10
max_aspectFLOAT1.500–10

Outputs (3)

NameTypeDescription
imageIMAGE
widthINT
heightINT