Nodes/ComfyUI_AspectRatioToSize/CalcFactorWidthHeight
ComfyUI Node

CalcFactorWidthHeight

The upscale-math helper that keeps your dimensions aligned

By sugarkwork·Created 2 years ago·Updated 2 months ago· 3
CalcFactorWidthHeight
    • width
    • height
    • large_side
    • small_side
    • width_float
    • height_float
    • large_side_float
    • small_side_float
    width
    height
    factor1.50
    divide1
    plus_dividetrue

    The node for "just multiply by 1.5 and make it a multiple of 64"

    Hires-fix and upscale workflows are full of tedious arithmetic. You've got a 768×512 latent, you want to upscale it 1.5×, and the result has to stay aligned to a multiple of 8 or 64 or the VAE - or Flux - complains. That's the exact problem CalcFactorWidthHeight solves: give it width, height, and a factor, and it returns the scaled dimensions in two flavors - raw floats and ints snapped to a multiple you choose.

    What it computes

    The source is refreshingly transparent. It scales both sides by factor, keeps the un-rounded float versions, then produces integer versions. If your divide value is greater than 1, those ints get aligned to the nearest multiple of divide - rounding up when plus_divide is on (the default), rounding down when it's off.

    So with divide=64 and plus_divide=true, 768×512 at factor 1.5 becomes 1152×768 exactly, and a fractional result like 1032.5 rounds up to 1088. With divide=1 (the default), no alignment happens - you just get int() of the floats.

    The inputs

    Four of them, and only two usually need touching:

    • width, height - the starting dimensions, often from SizeToWidthHeight or your current latent size.
    • factor - the multiplier, default 1.5. The usual hires-fix sweet spot is 1.5–2×.
    • divide - the alignment unit. Default 1 (no snapping). Set 8 for most VAE-decoded latents, or 64 on Flux.
    • plus_divide - round up (true, default) or down (false) when snapping.

    Outputs: why there are eight of them

    The pack returns both the aligned ints and the raw floats, because each is useful:

    • width, height, large_side, small_side - integers, aligned, ready to feed Empty Latent, an image resize, or a latent upscale node.
    • width_float, height_float, large_side_float, small_side_float - the exact un-rounded numbers, handy when you want to see what alignment cost you.

    The _float versions are the honest answer to "what would this be if I didn't have to round"; the ints are what you actually run.

    A note on documentation

    This is the quiet node in the pack - the README documents the other four and skips this one, so the code's docstring is the real source of truth. What it says, and what you should trust: a factor of 0 is treated as invalid, divide ≤ 1 means no alignment, and plus_divide controls up vs. down rounding. It's simple enough that "read the code" is a reasonable fallback, but you probably won't need to - the defaults work.

    Install

    It's in the same dependency-free repo as the rest:

    cd ComfyUI/custom_nodes
    git clone https://github.com/sugarkwork/ComfyUI_AspectRatioToSize
    

    Restart ComfyUI, or grab it from ComfyUI Manager ("AspectRatioToSize"). No models, no pip installs.

    Categoryimage

    Inputs (5)

    NameTypeDefaultDescription
    widthINT
    heightINT
    factorFLOAT1.50
    divideINT1
    plus_divideBOOLEANtrue

    Outputs (8)

    NameTypeDescription
    widthINT
    heightINT
    large_sideINT
    small_sideINT
    width_floatFLOAT
    height_floatFLOAT
    large_side_floatFLOAT
    small_side_floatFLOAT