π Image Constrain
Fit an image into a box without wrecking the aspect ratio
- images
- IMAGE
"Make this image fit 1024x1024" sounds simple until you do it by hand and realize fit can mean two different things: shrink it so the whole image is visible, or crop it so it fills the box exactly. π Image Constrain gives you both, plus the sane default in between. It's the node that handles the most common image-size chore in ComfyUI - normalizing inputs for a model that expects a fixed resolution - without you doing mental math about aspect ratios.
How it works
You feed in images (a batch is fine; it processes every image) and set the box with max_width / max_height (both default to 1024) and optionally min_width / min_height (default 0). The node rescales with LANCZOS to fit the largest size that stays within the max box while preserving aspect ratio - so a 1920x1080 image dropped into a 1024 max box comes out 1024x576, not squashed. The min values let you force small images up to a usable size.
Then the interesting toggle: crop_if_required. With it off (the default), the image just fits - which can leave you with letterboxing and a size that doesn't exactly match your box. Turn it on and the node instead scales to cover the box and crops the overflow, giving you an exact 1024x1024 out of any aspect ratio. The optional crop_position (center, top, bottom, left, right, default center) decides which part of the image survives the crop - invaluable when you're feeding faces or subjects that live off-center and a center crop would cut them.
Inputs and outputs
- images -
IMAGE, required. - max_width / max_height -
INT, default 1024. - min_width / min_height -
INT, default 0. - crop_if_required -
BOOLEAN, default off. - crop_position - enum, optional, default
center. - Output:
IMAGE.
Where you'd reach for it
This is your "normalize anything into what the model expects" node: pre-process an input image before img2img, before an IP-Adapter, or before a fixed-size controlnet preprocessor. The crop_position option is the quiet hero - when a preprocessor produces a face map or a control image, being able to say "keep the top, not the center" saves a lot of re-cropping.
Install
Part of hay86/ComfyUI_AceNodes:
cd ComfyUI/custom_nodes
git clone https://github.com/hay86/ComfyUI_AceNodes
cd ComfyUI_AceNodes
pip install -r requirements.txt
Restart ComfyUI and look for π
Image Constrain under Ace Nodes. Manager users search ComfyUI_AceNodes.
Gotchas
The obvious trap: setting min values that exceed max values produces nonsense sizes, so keep min β€ max. And crop_if_required off doesn't guarantee the output matches your box - if you need exact dimensions, that's what the crop mode is for. The pack's heavy requirements.txt (rembg, transformers, boto3β¦) installs regardless. Otherwise this is one of the most genuinely useful nodes in the pack, because "fit this image to a model's resolution" never stops being a thing you need.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| images | IMAGE | β | |
| max_width | INT | 1024 | β |
| max_height | INT | 1024 | β |
| min_width | INT | 0 | β |
| min_height | INT | 0 | β |
| crop_if_required | BOOLEAN | false | β |
| crop_positionopt | COMBO | center | 5 options: center, top, bottom, left, right |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | β |