get image ratio
Turn an image's dimensions into a usable aspect-ratio string
- images
- masks
- images
- masks
- max_HW
- min_HW
- ratio_float
- ratio_str
- ratio_class
get_image_ratio takes an image or mask and works out its aspect ratio in three different shapes: an exact float, a human-readable string like "16:9" rounded to the nearest sensible integer ratio, and a plain classification of whether it's square, wide, or tall. Handy for branching a workflow based on what shape your source material actually is, instead of hardcoding an assumption that breaks the moment someone feeds it a portrait image.
It's the sibling of get_image_data in ComfyUI-WJNodes' small GetData category - a corner of a much larger personal pack (807502278: "a simple node package that I use myself") that's mostly built around image cropping, video slicing, and color tools.
How it works
The node reads the height and width off whatever you connect, then computes max_HW/min_HW (the larger and smaller of the two dimensions), ratio_float (the exact width-to-height ratio), ratio_str (that same ratio snapped to the nearest common integer pairing, like "4:3" or "9:16"), and ratio_class - a simple 0/1/2 code for square, wide, or tall. If you connect both an image and a mask at once, image data takes priority for the calculation, same as its sibling node.
The inputs and outputs that matter
images/masks- both optional; connect one or both. Image wins if both are present.
Outputs: images and masks passed through unchanged, max_HW and min_HW (INT), ratio_float (the precise ratio), ratio_str (a nearest-integer string like "16:9"), and ratio_class (0 = square, 1 = wide, 2 = tall).
This pairs naturally with this pack's pixel-scaling nodes - feed a source image's ratio into a branch that picks a different target size preset depending on whether it's landscape or portrait, rather than forcing everything through one fixed aspect ratio.
How to install it
Through ComfyUI Manager, search "ComfyUI-WJNodes." Manually:
cd ComfyUI/custom_nodes
git clone https://github.com/807502278/ComfyUI-WJNodes.git
Restart ComfyUI. No models required - this is arithmetic on a tensor's shape, nothing more.
Common issues & troubleshooting
ratio_str shows a ratio that doesn't quite match your actual dimensions. That's expected - it's snapped to the nearest common integer ratio, not computed as an exact fraction reduction. A 1300×720 image is close enough to 16:9 to be labeled that way even though it isn't precisely 16:9; use ratio_float if you need the exact number.
ratio_class gives 0 (square) for something that looks slightly off-square to you. The classification threshold for "square" isn't documented beyond the three-way split - if you need finer control than square/wide/tall, use ratio_float directly and branch on your own threshold instead.
Both image and mask connected, but you needed the mask's ratio specifically. Same priority rule as get_image_data - image wins when both are present. Disconnect the image input, or run a second node with just the mask, if you need the mask's numbers specifically.
No outside writeups for this node. It's a small, low-risk introspection utility inside a personal pack with no real community discussion around it - but the math it does is simple enough to verify by eye against a source image if a number ever looks wrong.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| imagesopt | IMAGE | — | |
| masksopt | MASK | — |
Outputs (7)
| Name | Type | Description |
|---|---|---|
| images | IMAGE | — |
| masks | MASK | — |
| max_HW | INT | — |
| min_HW | INT | — |
| ratio_float | FLOAT | — |
| ratio_str | STRING | — |
| ratio_class | INT | — |