Nodes/ComfyUI-YCNodes/YC Constrain Image
ComfyUI Node

YC Constrain Image

Fit any batch of images inside max width/height — and know it's a list coming out

By yichengup·Created 2 years ago·Updated 4 months ago· 48
YC Constrain Image
  • images
  • IMAGE
max_width1024
max_height1024
min_width0
min_height0
crop_if_requiredno

YC Constrain Image is a resize node with one job: take any image and scale it down to fit inside max_width × max_height, keeping the aspect ratio. Feed it a batch of mismatched sizes and each frame comes back individually constrained. It's the "make everything pipeline-shaped" node you drop in front of a model that expects 1024×1024-ish inputs - SDXL, Flux, any VAE-sensitive step - when your source images are all over the place.

It also has min_width / min_height and crop_if_required, which together make it a two-sided clamp: shrink to fit the max, and if the result would fall below the min, scale up to the min and center-crop back down to the max if needed. That's the "constrain" behavior that plain resize nodes don't give you - most will happily hand a 200px image to a 1024 model. This one can refuse to let that happen.

How it works

Per image: compute the aspect ratio, scale to fit the max box (Lanczos, via PIL), and if crop_if_required is yes, upscale to satisfy the mins and center-crop any excess beyond the max. The min checks are sequential - width first, then height - so a wide image gets its width lifted to min_width and the height follows proportionally.

The inputs that matter

  • max_width / max_height (defaults 1024) - the box everything must fit in.
  • min_width / min_height (defaults 0) - the floor; only enforced when crop_if_required is yes.
  • crop_if_required - yes/no, default no. Turn it on and you get min enforcement + center-crop; leave it off and it's a pure fit-inside resize.

The gotcha that will bite you

The output is declared as a list, not a batch - OUTPUT_IS_LIST = True in the code. Each image comes out as a separate element, and that matters enormously for what you can plug into next. Nodes that consume a list-of-images (anything list-aware, or a loop) are fine; a plain batch consumer is not. If you're feeding this into a KSampler's latent pipeline directly, you'll likely need to convert the list back to a batch first. Read the wires before you wire it.

Install

Part of ComfyUI-YCNodes. ComfyUI Manager → search "ComfyUI-YCNodes", or:

cd ComfyUI/custom_nodes
git clone https://github.com/yichengup/ComfyUI-YCNodes

Restart ComfyUI. Standard deps, no downloads. Under "YCNode/Image".

Gotchas

Beyond the list output, two notes. crop_if_required cropping is center-only - if you need top-anchored crops, this isn't it. And there's no "only downscale" flag; with crop_if_required off it never upscales, but with it on, small images get upscaled to the min, which is usually what you want before a model. For the classic two-pass hi-res fix, remember this node is the fit-to-size pass; the generation side still needs its own sampler.

CategoryYCNode/Image

Inputs (6)

NameTypeDefaultDescription
imagesIMAGE
max_widthINT10241–8192
max_heightINT10241–8192
min_widthINT00–8192
min_heightINT00–8192
crop_if_requiredCOMBOno2 options: yes, no

Outputs (1)

NameTypeDescription
IMAGEIMAGE