Nodes/ComfyMath-NG/AspectRatioResolution
ComfyUI Node

AspectRatioResolution

Stop guessing resolutions — this node turns an aspect ratio and a megapixel target into exact width and height

By rabanti-github·Created 2 months ago·Updated 6 days ago· 0
AspectRatioResolution
    • width
    • height
    aspect_ratio
    megapixels1.00
    multiple16

    You know the ritual. New checkpoint downloaded, you need a size, so you type "best Flux resolution" into your search bar for the tenth time or grab some preset off a Reddit thread and hope it's right. ComfyMath's AspectRatioResolution is the node that makes you stop doing that. You pick an aspect ratio, tell it how many megapixels you want, and it hands back exact width and height integers. Done.

    It belongs to ComfyMath - the maintained fork (published as ComfyMath-NG) of Evan Spearman's original ComfyMath, that ancient math-utility staple that went quiet around 2024. This fork keeps every CM_ node identifier so old workflows load unchanged, and it's where the resolution calculators live. It's pure math: no model downloads, no API keys, no calls out. The only runtime dependency is numpy, which ComfyUI already ships, so the install is trivial.

    How it works

    The math is a square root and a rounding step. A "megapixel" here means exactly 1024×1024 = 1,048,576 pixels, so 1 MP in a 1:1 ratio comes out as precisely 1024×1024 - a nice property that makes the numbers behave. The node computes an ideal width and height that hit your target pixel count at the chosen ratio, then snaps each to the nearest multiple of multiple.

    That rounding is the one thing to internalize: you get near your target, not exactly it. 16:9 at 1 MP with a multiple of 16 snaps to 1360×768, which is about 1.04 MP. That's fine - that's the point. It's a starting size, and the rounding is what keeps the result legal for your model's divisibility rule.

    The inputs that matter

    • aspect_ratio - eleven options from 1:1 and 4:3 out to 16:9, 21:9, and their portraits (9:16, 9:21, and so on).
    • megapixels - float, defaults to 1.0, and this is the knob you'll actually turn. SD 1.5 wants ~0.25 MP (512²), SDXL ~1 MP, Flux.1 ~1 MP, and Flux.2 is happiest somewhere between 1 and 2 MP.
    • multiple - pixel divisibility, defaults to 16. Use 16 for Flux, 64 if you're on an SDXL-family model that likes it.

    Wire width and height into an Empty Latent Image (right-click and convert those two widgets to inputs) or straight into whatever size socket your model's loader exposes.

    The broader context: the old SDXL-era habit was "generate at one of the trained ratios - 1024², 1152×896, 1344×768 - then upscale." Most 2026 models have loosened up and care about a megapixel band plus a divisibility rule rather than one canonical preset, which is exactly the shape this node fits. It's the modern answer to the resolution question.

    Installing it

    Grab it through ComfyUI Manager - search "ComfyMath" and install the one published as ComfyMath-NG - or clone it by hand:

    cd ComfyUI/custom_nodes
    git clone https://github.com/rabanti-github/ComfyMath.git ComfyMath-NG
    

    Restart ComfyUI afterward. The one gotcha that bites people: this fork registers the same node identifiers as the original ComfyMath. If you still have evanspearman's copy installed, the two collide. Disable or delete the original first, and clone into a folder named ComfyMath-NG to keep them straight.

    Things that will annoy you

    • The node won't tell you if your ratio is a bad idea for your model. 21:9 at 1 MP comes out to 1568×672, but a model fine-tuned mostly on square-ish images may produce anatomy crimes at that ratio regardless of the math. The math is correct; taste is on you.
    • Rounding to the multiple can land slightly over or under your target megapixels - expected, not a bug.
    • multiple is capped at 512 and megapixels won't go below 0.01, which is plenty of headroom, but know the limits exist so a wildly typed value doesn't surprise you.
    Categorymath/graphics

    Inputs (3)

    NameTypeDefaultDescription
    aspect_ratioCOMBO11 options: 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, +5
    megapixelsFLOAT1.00
    multipleINT161–512

    Outputs (2)

    NameTypeDescription
    widthINT
    heightINT