Nodes/ComfyUI_AspectRatioToSize/MatchImageToAspectRatio
ComfyUI Node

MatchImageToAspectRatio

Know an image's ratio without eyeballing it

By sugarkwork·Created 2 years ago·Updated 2 months ago· 3
MatchImageToAspectRatio
  • image
  • ratio
  • ratio_w
  • ratio_h
ratio_16_9true
ratio_9_16true
ratio_4_3true
ratio_3_4true
ratio_3_2true
ratio_2_3true
ratio_1_1true
aspect_ratio16:9, 9:16

The "what ratio is this thing?" node

You've got an image - a reference you're img2img-ing, an upscaled result you want to extend - and you need to know its aspect ratio, rounded to something you can actually generate at. You could squint at 1534×1032 and think about it. Or you could feed it to MatchImageToAspectRatio and get back "16:9" plus the 16 and 9 as separate numbers.

That's the pitch: it reads a real image, compares its actual width÷height against a list of candidate ratios, and returns the closest match as a string and two ints. The README's intended workflow is: match an image's ratio, hand that ratio string to AspectRatioToSize with your preferred long side, and you get the exact rectangle to regenerate at the same shape.

How it works

The node grabs the image's dimensions, computes width / height, then walks your enabled candidates and keeps whichever has the smallest |actual_ratio − candidate_ratio|. Pure nearest-neighbor by number.

Three things shape the candidate list:

  • Seven checkbox toggles, all on by default: ratio_16_9, ratio_9_16, ratio_4_3, ratio_3_4, ratio_3_2, ratio_2_3, ratio_1_1. Uncheck the ones you never want it to land on.
  • A free-text aspect_ratio field (default "16:9, 9:16") for ratios that aren't in the list - comma-separated, and the code tolerates ;, /, . and newlines as separators too.
  • Duplicates don't matter; the node de-duplicates internally.

Outputs: ratio (the "16:9" string), plus ratio_w and ratio_h (the 16 and 9). The string is the thing you wire into AspectRatioToSize.

The gotcha to know before you trust it

Because it's pure nearest-neighbor, a middling image can land on a ratio you didn't intend. A 1.4:1 image, for instance, is closer to 4:3 (1.333) than 3:2 (1.5), so that's what you get. There's no "within tolerance" threshold - it always picks something. If a 4:3 answer is annoying you, uncheck ratio_4_3 and it'll go to 3:2 instead. That toggle list is the real control surface; treat it as the filter it is.

One more thing to know: the default aspect_ratio string ("16:9, 9:16") overlaps with the checkboxes that are already on. That's harmless - the dedup handles it - but it means you're never double-counting a ratio no matter how you enable it.

Install

Same repo as the rest of the pack:

cd ComfyUI/custom_nodes
git clone https://github.com/sugarkwork/ComfyUI_AspectRatioToSize

Restart ComfyUI (or use ComfyUI Manager and search "AspectRatioToSize"). No pip dependencies, no models to download - the pack needs nothing beyond what ComfyUI already has.

Categoryimage

Inputs (9)

NameTypeDefaultDescription
imageIMAGE
ratio_16_9optBOOLEANtrue
ratio_9_16optBOOLEANtrue
ratio_4_3optBOOLEANtrue
ratio_3_4optBOOLEANtrue
ratio_3_2optBOOLEANtrue
ratio_2_3optBOOLEANtrue
ratio_1_1optBOOLEANtrue
aspect_ratiooptSTRING16:9, 9:16

Outputs (3)

NameTypeDescription
ratioSTRING
ratio_wINT
ratio_hINT