ImageAspectResolution
Match any image's aspect ratio automatically — ImageAspectResolution computes width and height from a target megapixel count
- image
- width
- height
This is the sibling of ComfyMath's AspectRatioResolution, and for img2img work it's usually the more useful one. Instead of picking an aspect ratio from a dropdown, you feed it an image, and it derives the ratio from that image, then computes width and height for a target megapixel count. Same idea, zero thinking about "what ratio is my source actually?"
The use case that makes it click: you're doing img2img on a photo that's, say, 4:3, or some weird panorama that's 2.39:1, or a phone shot that's not any preset ratio at all. You want the output to keep the source's shape but land at a sane pixel budget. Drop that same image into ImageAspectResolution, set megapixels to 1.0, and out come width and height that preserve the source's ratio at about 1 MP. No calculator, no matching the ratio against a list.
How it works
Mechanically it's nearly identical to AspectRatioResolution - the code path is shared. It reads the tensor's spatial dimensions (width and height from image.size()), computes the aspect ratio, then does the same square-root dance: target pixels = megapixels × 1024², ideal width and height from the ratio, each rounded to the nearest multiple of multiple. So 1 MP in a 1:1 ratio gives exactly 1024×1024, and oddball ratios get snapped to the nearest legal multiple.
The inputs that matter
- image - any IMAGE tensor. Wire in the source image itself, or any reference image whose shape you want to copy. It doesn't have to be the image being generated; it just has to have the ratio you want.
- megapixels - float, defaults to 1.0. This is the output size target; ~0.25 MP for SD 1.5 territory, 1 MP for SDXL and Flux.1, 1–2 MP for Flux.2.
- multiple - pixel divisibility, defaults to 16. Bump to 64 for SDXL-family models, keep 16 for Flux.
Outputs are width and height as plain integers, ready to feed an Empty Latent Image (convert its width/height widgets to inputs first) or any model-specific size node.
Where people get burned: the megapixel value is a target, not a promise. Because dimensions snap to the multiple, a panorama at 1 MP might come out a bit over or under a true megapixel - that's the trade you're making for a size your model will accept. And as with its sibling, the node computes the geometry but doesn't sanity-check it against your checkpoint, so a source ratio that's wildly different from what your model trained on is still on you.
Installing it
It ships in ComfyMath (published as ComfyMath-NG). Via ComfyUI Manager, search "ComfyMath" and pick that one, or clone it:
cd ComfyUI/custom_nodes
git clone https://github.com/rabanti-github/ComfyMath.git ComfyMath-NG
Restart ComfyUI. This fork registers the same node identifiers as the original ComfyMath, so remove or disable the original first - they don't coexist. The only dependency is numpy, already present; there are no model files to download.
The one habit worth building
If you do much image-to-image work, this node quietly removes a whole class of fiddling. Before, "keep the ratio, change the size" meant doing the arithmetic yourself or hoping a preset matched. Now it's a wire from your loaded image into a node you never have to think about again.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| megapixels | FLOAT | 1.00 | — |
| multiple | INT | 161–512 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| width | INT | — |
| height | INT | — |