Image Ratio Detector (UTK)
Ask any image its aspect ratio and get an answer you can wire into a resize
- image
- ratio_str
- width
- height
- approx_ratio_str
Every "keep the aspect ratio" workflow starts with the same question: what is the aspect ratio of this image? If you've done it by hand you know the drill - open the image, check the pixel dims, divide, realize 1349×752 doesn't map to anything neat. Image Ratio Detector (UTK) reads the dimensions for you and hands back both the exact ratio and the nearest "standard" ratio, which is genuinely more useful than it sounds.
It's one of the smaller nodes in ComfyUI-UniversalToolkit, and it doesn't pretend to be more than it is: a tiny detection utility with zero settings. You can't misconfigure it, which is refreshing.
How it works
The node looks at the image tensor's dimensions, reduces the width/height to a GCD-simplified ratio, and returns it as a string like 3:2 or 1349:752. Then it does the thing that makes it actually handy: it compares your exact ratio against a table of standard ratios - 1:1, 16:9, 4:3, 3:2, 2:3, 3:4, 9:16, 5:4, 21:9, 2:1, the cinematic 1:1.85 and 1:2.35, and a few more - and picks the nearest one for approx_ratio_str. So a 1349×752 image reports approx_ratio_str of 16:9 even though its exact ratio_str is a mess.
That approximate output is the gem. It's the value you want when you're feeding a resolution into a node that takes ratio presets, or deciding whether to build a landscape or portrait canvas. The exact string is for when you actually need the truth, or want to display it.
Inputs and outputs
image(IMAGE) - the only input. It reads the first frame if you hand it a batch.
Outputs:
ratio_str(STRING) - exact reduced ratio, e.g.3:2.width(INT) andheight(INT) - the pixel dimensions, which you'll often need anyway. Free money.approx_ratio_str(STRING) - nearest standard ratio, orN/Aif the image is degenerate (zero dimension).
Wire width and height into anything that needs numbers - an Empty Latent, a resize target, a math node.
Install and gotchas
Install via ComfyUI Manager (search "ComfyUI-UniversalToolkit") or:
cd ComfyUI/custom_nodes
git clone https://github.com/whmc76/ComfyUI-UniversalToolkit
pip install -r requirements.txt
Restart ComfyUI. No models, no keys - just torch and math.
Gotchas are minimal, because there's almost nothing to get wrong. One thing to know: approx_ratio_str picks the closest standard ratio by numeric closeness, so a square-ish 1.2:1 image will report as 1:1 even though it isn't exactly square - that's the intended tradeoff of "approximate." And if you feed it a latent instead of an image, it won't error loudly; it'll just produce a nonsense ratio, so keep the input type honest. For a beginner chasing "why does my resize distort things," this node plus an aspect-ratio-preserving resize is the whole answer.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| ratio_str | STRING | — |
| width | INT | — |
| height | INT | — |
| approx_ratio_str | STRING | — |