Nodes/ComfyUI-Custom-Scripts/Constrain Image 🐍
ComfyUI Node Runs on cloud

Constrain Image 🐍

Cap an image's size without upscaling ones that are already small

By pythongosssssΒ·Created 3 years agoΒ·Updated 6 months agoΒ· 3,156
Constrain Image 🐍
  • images
  • IMAGE
β—„max_width1024β–Ί
β—„max_height1024β–Ί
β—„min_width0β–Ί
β—„min_height0β–Ί
β—„crop_if_requirednoβ–Ί

Constrain Image is a size normalizer: it takes whatever image (or batch of images) you throw at it and makes sure the result sits between a minimum and maximum size before it hits whatever comes next. That "whatever comes next" is usually something that cares a lot about dimensions - a ControlNet preprocessor, an IPAdapter encoder, or a sampler that'll happily eat your VRAM if you let a user-uploaded 6000px image straight through.

It's part of ComfyUI-Custom-Scripts, pythongosssss's utility pack. Nothing fancy going on here - this is plumbing, not a model - but plumbing that saves you a real headache when you're accepting arbitrary image input.

How it works

Four numbers do the work. max_width and max_height (both default 1024) are the ceiling - anything bigger gets scaled down to fit. min_width and min_height (both default 0, effectively no floor out of the box) are the opposite - anything smaller gets scaled up. If the image already sits inside that window, the node leaves it untouched.

crop_if_required (yes/no, default no) exists because those four bounds don't always agree with the image's own aspect ratio. Hit the max on one axis and the min on the other at the same time, and there's no single scale factor that satisfies both without cropping something. Leave it off and the node does its best purely by scaling - which can mean one of your bounds doesn't land exactly. Switch it on and the node crops the excess to hit the target precisely.

The images input takes IMAGE, and the output is IMAGE as a list - it handles a batch at once, each image constrained independently, so this drops cleanly into a multi-image pipeline without extra looping.

What this is and isn't

This is a "make it fit" node, not a quality upscaler. There's no ESRGAN model in here, no generative fill, just resize and crop math. If a source image is already soft, blurry, or low-detail, Constrain Image isn't going to fix that - it'll just hand you a bigger or smaller version of the same soft image. Reach for a real upscale model (and the node that loads one) when detail is the problem; reach for this one when dimensions are the problem.

Installing it

  • ComfyUI Manager - search "ComfyUI-Custom-Scripts", install, restart.
  • Manual - cd ComfyUI/custom_nodes && git clone https://github.com/pythongosssss/ComfyUI-Custom-Scripts, restart.

No models, no extra dependencies - pure image math.

Common issues

Image comes out distorted or stretched. crop_if_required is off and your min/max bounds don't agree with the source's aspect ratio, so the node's scale-only approach ends up with a mismatch. Turn cropping on, or loosen one of the bounds so they stop fighting each other.

Nothing appears to change. The image was already inside your min/max window on both axes. That's the node working correctly, not a bug - it only touches images that are outside the bounds.

Getting an unexpected crop. crop_if_required is on and something is trimming to satisfy a bound you didn't realize was actually binding - check both min and max on both axes, not just whichever one you set on purpose. It's easy to forget the default min is 0, but a nonzero max on the opposite axis can still force a crop depending on aspect ratio.

Categoryimage

Inputs (6)

NameTypeDefaultDescription
imagesIMAGEβ€”
max_widthINT1024β€”
max_heightINT1024β€”
min_widthINT0β€”
min_heightINT0β€”
crop_if_requiredCOMBOno2 options: yes, no

Outputs (1)

NameTypeDescription
IMAGEIMAGEβ€”