Nodes/m10i/Calculate Optimize Resolution
ComfyUI Node

Calculate Optimize Resolution

The node that does the aspect-ratio math so you don't have to

By 0nyx-networks·Created 7 months ago·Updated 4 months ago· 0
Calculate Optimize Resolution
    • width
    • height
    input_width1024
    input_height1024
    desired_width1024
    desired_height1024

    You've got an input image at, say, 2048×1024 and you want to resize it to live inside a 1024×1024 box without distorting it. That's a one-liner in a calculator - width 1024, height 512 - but when you're doing it in your head for every image, or wiring it into a workflow that needs to handle arbitrary inputs, you start wishing a node would just hand you the numbers. That's exactly what Calculate Optimize Resolution does. It's the "helping hand" part of the pack's name doing what it says on the tin.

    How it works

    The node takes your source image's size and a target size, then does the classic fit-inside-a-bounding-box calculation. From the source:

    • It computes the aspect ratio: input_width / input_height.
    • It tries two candidate resolutions: one fitted to desired_width (height = round(width ÷ aspect)), one fitted to desired_height (width = round(height × aspect)).
    • It picks whichever candidate is closer to the target, measured by Manhattan distance.

    So the output always fits inside the box on at least one axis and never overflows the other - you get a letterboxed fit, not a crop. Feed the result into Empty Latent Image or a resize node and you've got a resolution that preserves the source's proportions.

    The inputs that matter

    Four integers, all obvious:

    • input_width, input_height - the size of your source image. If you've got a GetImageSize-style node in your graph, wire those in so the node adapts to whatever comes through.
    • desired_width, desired_height - the bounding box you want to fit inside. 1024×1024 is the classic default.

    The outputs are just width and height (both INT), ready to feed an empty latent or a resize.

    The gotcha nobody mentions

    The node rounds to the nearest integer, full stop. It does not snap to multiples of 8, 16, or 64. Most latent-based models want dimensions divisible by at least 8 (Flux flatly requires multiples of 64, per the resolution rules every architecture carries), so a 3:2 image fitted into 1024×1024 gives you 1024×683 - and 683 is not latent-friendly. Treat the output as a starting point: round to a friendly multiple yourself, or accept odd dimensions for models that tolerate them. It's a suggestion engine, not a latent-safe bucket.

    Install

    ComfyUI Manager → search "comfy-nekonote-extensions", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/0nyx-networks/comfy-nekonote-extensions
    

    Then restart ComfyUI. It's a pure Python utility pack from Japanese dev MINETA "m10i" Hiroki (MIT licensed, currently 0.4.7) - no model files, no GPU memory, just httpx, pyyaml, pillow, piexif, diskcache, and numpy, most of which ComfyUI already ships. You'll find the node under NEKONOTE → Utils. If the node doesn't run, the usual suspects apply: a stale ComfyUI (this pack uses the newer comfy_api.latest backend API) or a missing dependency - let Manager install the requirements.

    CategoryNEKONOTE/Utils

    Inputs (4)

    NameTypeDefaultDescription
    input_widthINT1024
    input_heightINT1024
    desired_widthINT1024
    desired_heightINT1024

    Outputs (2)

    NameTypeDescription
    widthINT
    heightINT