Nodes/Comfy swap and scale/Swap width/height and/or scale image
ComfyUI Node

Swap width/height and/or scale image

Swap width and height, snap to 32, cap at 1MP — the resolution math nobody wants to do by hand

By MijnSpam·Created about a year ago·Updated about a year ago· 0
Swap width/height and/or scale image
    • out_height
    • out_width
    IN_HEIGHT
    IN_WIDTH
    Swap_Width_Heightfalse
    factor_32pxtrue
    Limit_1MPtrue

    ComfyUI is full of nodes that make pixels and oddly short on nodes that do the boring arithmetic around them. SwapAndScale is the boring arithmetic. You feed it a width and a height, it swaps them, snaps them to multiples of 32, and/or caps the total at one megapixel - then hands back two integers. That's the whole job. No model downloads, no VRAM, no dependencies. The name says "swap and scale," but the scaling direction is important: it only scales down to fit the 1MP cap. It will never upscale anything.

    Why you'd reach for it

    Every model has a native resolution, and ComfyUI does not care what yours is. Generate far off it and you get stretched anatomy, tiling, or double heads; the classic fix is "generate at native resolution, upscale after." SwapAndScale is a cheap way to keep your canvas in the safe band. The classic wiring goes:

    GetImageSize (or ImageSize node) → SwapAndScale → EmptyLatentImage → KSampler
    

    You take the dimensions of a source image, run them through this node, and feed the result into an empty latent - so your img2img canvas matches the source's aspect ratio without accidentally blowing past the model's comfort zone. The swap toggle is what makes it one node instead of two: grab a landscape source, flip the dimensions, and you've got a portrait canvas of roughly the same area.

    The three toggles (that's the whole UI)

    Two inputs (IN_WIDTH, IN_HEIGHT) and three booleans, all of which are obvious from their names:

    • Swap_Width_Height - swaps the two values. The one you reach for going landscape ↔ portrait.
    • factor_32px - rounds each side down to the nearest multiple of 32 (so 1000 becomes 992). All the SDXL trained aspect ratios - 1152x896, 1216x832, 1344x768, 1536x640 - are multiples of 32, so this keeps you on friendly ground for the whole SDXL family.
    • Limit_1MP - if width × height exceeds the cap, scales both sides proportionally until the product fits.

    The outputs are just out_height and out_width, both INTs, wired straight into EmptyLatentImage or an ImageScale node. That's it.

    Installing it

    It's one file and has zero dependencies - the README says "no dependencies, all built-in python functions," and the source agrees: pure % and int() math. Install via ComfyUI Manager by searching "Comfy swap and scale", or clone it by hand:

    cd ComfyUI/custom_nodes
    git clone https://github.com/MijnSpam/ComfyUI_SwapAndScale
    

    Then restart ComfyUI (or hit Reload Custom Nodes) and refresh the browser. No requirements.txt, no models, no GPU memory to budget. It's part of a small "KB comfy" family from the same author (MijnSpam, publisher id kbrambo), whose other node is an upload-to-Pushover utility - this is a hobby-scale pack, and it shows in a good way: small, focused, MIT-licensed.

    Where people get burned

    A few things the README glosses over. First, rounding is always down - you can lose up to 31 pixels per side, and since the 32-snap happens before the 1MP check, a 1000x1000 input becomes 992x992 before anything else happens. Fine for SDXL, annoying if you were aiming for exact dimensions.

    Second, "1MP" means 1024×1024 = 1,048,576 pixels, not a round million - a changelog entry ("1.0.1 Fixed fault in 1MP resolution, now it actually uses 1024*1024") confirms the author knows exactly what it does. Third, the same snapping that keeps you safe on SDXL can bite on models that want multiples of 64 - Flux, for instance. The node happily hands you 736×1376 (both multiples of 32, neither of 64). The aspect ratio also drifts a little because each side is snapped independently after scaling; the README admits the result is "not perfect."

    None of that makes it a bad node. It's a calculator - the trick is knowing the answer it gives is a safe one, not a precise one. If you need exact dimensions or 64-multiples, do the math yourself or use ComfyUI's own ImageScaleToTotalPixels. But for the 90% case - "let me generate close to this image's proportions without blowing the budget" - this is the fastest two-integer shortcut around.

    CategoryImage

    Inputs (5)

    NameTypeDefaultDescription
    IN_HEIGHTINT
    IN_WIDTHINT
    Swap_Width_HeightBOOLEANfalse
    factor_32pxBOOLEANtrue
    Limit_1MPBOOLEANtrue

    Outputs (2)

    NameTypeDescription
    out_heightINT
    out_widthINT