MatchImageToAspectRatio
Know an image's ratio without eyeballing it
- image
- ratio
- ratio_w
- ratio_h
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_ratiofield (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.
Inputs (9)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| ratio_16_9opt | BOOLEAN | true | — |
| ratio_9_16opt | BOOLEAN | true | — |
| ratio_4_3opt | BOOLEAN | true | — |
| ratio_3_4opt | BOOLEAN | true | — |
| ratio_3_2opt | BOOLEAN | true | — |
| ratio_2_3opt | BOOLEAN | true | — |
| ratio_1_1opt | BOOLEAN | true | — |
| aspect_ratioopt | STRING | 16:9, 9:16 | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| ratio | STRING | — |
| ratio_w | INT | — |
| ratio_h | INT | — |