Nodes/zhangp365/ComfyUI-utils-nodes/Match Image Ratio to Standard Size
ComfyUI Node

Match Image Ratio to Standard Size

Snap an odd-aspect image to the nearest SDXL training bucket

By zhangp365·Created 2 years ago·Updated 5 months ago· 134
Match Image Ratio to Standard Size
  • image
  • standard_width
  • standard_height
  • min
  • max
width_offset0
height_offset0

SDXL and most SDXL-derived checkpoints weren't trained on arbitrary resolutions - they were trained on a fixed set of bucketed sizes, all close to one megapixel, ranging from square out to fairly wide or fairly tall. Feed an SDXL pipeline a resolution outside that bucket list and you'll usually get more distortion and worse composition than the model is capable of. MatchImageRatioToPreset looks at an image you already have and tells you which official bucket its aspect ratio is closest to.

What it's for

The typical spot this earns its place: you've got a reference or input image at some odd resolution - a phone photo, a scraped web image, an upscale output that didn't land on a clean size - and you want to resize it for an SDXL-based img2img, inpaint, or ControlNet pass without fighting the model's own training distribution. Rather than eyeballing "which standard size is this closest to," this node does the ratio math for you and hands back the exact target dimensions to resize into.

How it works

It reads your image's width and height, computes the aspect ratio, and compares that against the fixed list of SDXL's standard training resolutions to find the closest match - not the closest total pixel count, the closest shape. That's the useful distinction: it's solving "what aspect ratio bucket does this belong in," which is the thing that actually matters for how well the model composes, not just "what's a round number near this size."

The inputs and outputs that matter

  • image (IMAGE) - the image whose ratio you want matched.
  • width_offset / height_offset (default 0, range -128 to 128, step 8) - nudge the matched preset by a fixed amount in either dimension after the match is found. The step size of 8 isn't arbitrary - VAE-based pipelines expect dimensions divisible by 8, and this keeps your offset from breaking that.

Four outputs: standard_width / standard_height (the matched bucket's dimensions, after any offset is applied) - feed these straight into a resize node ahead of your sampler. Plus min and max - the smaller and larger of the two matched dimensions, which is convenient if a downstream node wants "short edge" / "long edge" rather than caring which one is width versus height.

How to install it

Via ComfyUI Manager: search "ComfyUI-utils-nodes," install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/zhangp365/ComfyUI-utils-nodes

Restart ComfyUI. Pure arithmetic against a fixed lookup table - no models, no dependencies, fast install.

Common issues & troubleshooting

This node tells you the target size - it doesn't resize your image for you. You still need a resize/scale node downstream fed by standard_width/standard_height to actually produce the resized image; skipping that step and expecting this node's IMAGE input to come back changed is the most likely first mistake.

Also worth knowing: matching aspect ratio to a bucket doesn't mean your image's content fits that shape gracefully. If your source is a wildly different ratio than anything in the SDXL bucket list - a tall phone screenshot going into a much closer-to-square target, for instance - the resize step afterward will still need to crop or pad to get there, and this node isn't the thing making that call for you. Think of it as "which bucket should I aim for," with the actual crop/pad/stretch decision left to whatever resize node you pair it with. And if you're working with a non-SDXL model that has its own different set of native training resolutions (a lot of newer checkpoints do), this node's bucket list won't match - it's specifically tuned to SDXL's standard sizes, not a generic aspect-ratio-snapping tool.

Categoryutils/image

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
width_offsetINT0-128–128
height_offsetINT0-128–128

Outputs (4)

NameTypeDescription
standard_widthINT
standard_heightINT
minINT
maxINT