Nodes/ComfyUI-PredefinedResolution/Snap to Nearest Aspect Ratio
ComfyUI Node

Snap to Nearest Aspect Ratio

Snap resolutions to a clean aspect ratio before the model ever sees them

By pipelet-ai·Created 6 months ago·Updated 4 months ago· 0
Snap to Nearest Aspect Ratio
    • width
    • height
    • width_height_ratio
    • height_width_ratio
    width512
    height512
    output_resolution1080p
    enable_21_9true
    custom_21_92560x1080
    enable_16_9true
    custom_16_91920x1080
    enable_1_1true
    custom_1_11080x1080
    enable_9_16true
    custom_9_161080x1920
    enable_9_21true
    custom_9_211080x2560
    enable_4_3true
    custom_4_31440x1080
    enable_3_4true
    custom_3_41080x1440

    The display name is "Snap to Nearest Aspect Ratio," and it's the main event in the pipelet-ai/ComfyUI-PredefinedResolution pack. You feed it any width/height pair - from an uploaded image, a video loader, or your own head - and it works out which standard aspect ratio the input is closest to, then hands you clean dimensions at a resolution tier you pick: 720p, 1080p, 2K, or 4K.

    Why bother? Diffusion models are trained in aspect-ratio buckets. A 1837x1033 latent isn't in any bucket, and off-bucket sizes are where you get blurry edges, weird crops, or an out-of-memory that has nothing to do with VRAM and everything to do with one ugly dimension. This node normalizes that before sampling. Think of it as the sizing step you'd otherwise do by hand every time you swap an input image.

    How it works

    Pure math, no AI involved. The node computes your input ratio (width / height), finds the closest enabled preset among 21:9, 16:9, 4:3, 1:1, 3:4, 9:16, and 9:21, then sizes from the short edge: the short dimension becomes your tier (720, 1080, 2048, or 3840) and the long edge is round(short × ratio).

    • 1080p, 16:9 → 1920x1080
    • 1080p, 21:9 → 2520x1080
    • 2K, 21:9 → 4779x2048

    Worth knowing: the repo README's example claims 1920x800 at 2K 21:9 becomes "4608x2048" - that math is wrong, and the README also describes a target_ratio input the shipped code doesn't have. Trust the node's UI and the short-edge rule, not that README.

    The inputs that matter

    • width, height (INT) - your starting point. Defaults to 512, range 64–8192.
    • output_resolution - the tier. 720p, 1080p, 2K, 4K, or Custom. This is a resolution ladder, not a "keep my pixel count" tool: 512x512 in, 1080p out, means 1080x1080 out.
    • enable_21_9enable_3_4 (BOOLEAN) - seven toggles, all on by default. Turn off ratios you never generate so an input doesn't snap somewhere you don't want.
    • custom_21_9custom_3_4 (STRING) - only read when output_resolution is Custom. Format is "2560x1080" with an x. That's a real trap: type a comma and the parse silently fails.

    Outputs are width, height (INT) plus width_height_ratio and height_width_ratio (FLOAT). Wire the ints into an EmptyLatentImage (or an upscale node's size inputs); the ratios are handy for downstream nodes that want an aspect ratio as a float instead of a pair of ints.

    Where the two gotchas hide

    First, "4K" means short edge 3840. On 21:9 that's 8960x3840 - a 34-megapixel latent that will melt your sampler. The presets only guarantee the short edge, not a sane total pixel count, so don't reflexively pick the top tier on an ultrawide ratio.

    Second, disable every enable_* toggle and the code silently falls back to 1:1 rather than erroring. You'll notice eventually, but it won't tell you.

    Install

    Nothing heavy here - pure Python, no pip dependencies, no model downloads:

    cd ComfyUI/custom_nodes
    git clone https://github.com/pipelet-ai/ComfyUI-PredefinedResolution.git
    

    Then restart ComfyUI. Or grab it through ComfyUI Manager by searching "PredefinedResolution."

    Troubleshooting

    • It's not an image node. No image input, no resizing happens. It takes two integers and returns two integers - if you were expecting it to upscale your pixels, you want an upscaler (see the KB's upscaling essay) with this feeding it dimensions.
    • Custom strings are "WxH" - "2560x1080", not "2560,1080". A malformed string doesn't throw; you just silently get 1080p-tier output for that ratio.
    • You don't see target_ratio or width_height_scale - right, this build doesn't have them. The README is stale; the UI is truth.

    This is a genuinely useful little utility for anyone doing multi-ratio automation or video work, where inconsistent input sizes are the norm. It won't win awards for ambition, but it saves you from hand-typing dimensions five times a day.

    Categoryutils

    Inputs (17)

    NameTypeDefaultDescription
    widthINT51264–8192
    heightINT51264–8192
    output_resolutionCOMBO1080p5 options: 720p, 1080p, 2K, 4K, Custom
    enable_21_9optBOOLEANtrue
    custom_21_9optSTRING2560x1080
    enable_16_9optBOOLEANtrue
    custom_16_9optSTRING1920x1080
    enable_1_1optBOOLEANtrue
    custom_1_1optSTRING1080x1080
    enable_9_16optBOOLEANtrue
    custom_9_16optSTRING1080x1920
    enable_9_21optBOOLEANtrue
    custom_9_21optSTRING1080x2560
    enable_4_3optBOOLEANtrue
    custom_4_3optSTRING1440x1080
    enable_3_4optBOOLEANtrue
    custom_3_4optSTRING1080x1440

    Outputs (4)

    NameTypeDescription
    widthINT
    heightINT
    width_height_ratioFLOAT
    height_width_ratioFLOAT