ComfyUI Node

Megapixel Scale

The polite way to normalize any resolution to a megapixel budget

By Ferocit·Created about a year ago·Updated 3 days ago· 0
Megapixel Scale
    • width
    • height
    width512
    height512
    megapixels1.00

    Megapixel Scale is a two-integer math node hiding in a pack that mostly does text stuff. Feed it a width and a height, tell it how many megapixels you actually want, and it hands you back a new width/height pair scaled to that pixel budget with the aspect ratio intact. That's the whole thing - and "that's the whole thing" is why you'll keep reaching for it.

    Here's the situation it solves. Models these days don't want a single resolution, they want a band: SDXL was trained on a few specific aspect ratios around 1MP, Flux is happy at 1024+, and a lot of 2026 models accept any size in a 1–2MP range and just get soft if you wander off. The failure mode when you generate at the wrong size is stretched bodies and tiled patterns, which is exactly what the "you are using a model trained on 1024x1024 images" meme warns about. Megapixel Scale is the sanity step that stops you typing 1473x861 into a latent node and crossing your fingers.

    How it works

    The mechanism is embarrassingly simple, which is a compliment. It computes a single scale factor from the square root of target pixels divided by current pixels, applies it to both dimensions, then rounds each to the nearest multiple of 8:

    scale_factor = math.sqrt((megapixels * 1_000_000) / (width * height))
    new_width  = round(width  * scale_factor / 8) * 8
    new_height = round(height * scale_factor / 8) * 8
    

    Because one factor drives both sides, the aspect ratio is preserved exactly before rounding - and the rounding to multiples of 8 is the part that matters. Latent-space encoders expect dimensions divisible by 8 (Flux wants 64, worth remembering), and this guarantees it. A 1536x1024 image scaled to 1MP comes out 1224x816, which is 998,784 pixels - a hair under the target. The rounding means it's never pixel-exact, but within a couple percent is exactly where you want to be.

    The inputs that matter

    There are only three, all on the node:

    • width / height - the resolution you're starting from, defaults to 512 each. Wire these from an image's dimensions, a text node, or just type them.
    • megapixels - the target budget, default 1.0. This is the one you actually tune: 1.0 for SDXL-class models, up to 2.0 for the newer models that train in a higher band.

    It outputs width and height as two INTs. Notice what's not here: no image in, no image out. This node never touches pixels. You take those two integers and feed them into an Empty Latent Image, an image resize, or anything else that wants a dimension pair. New users get burned on exactly this - there's no preview because there's nothing to preview.

    Installing it

    It ships in comfyui-feroccustomnodes, a small pack by Ferocit that also includes Load Description, Random Line From Text, and Text Template. Easiest path is ComfyUI Manager - search "comfyui-feroccustomnodes" and install. Otherwise:

    cd ComfyUI/custom_nodes/
    git clone https://github.com/Ferocit/comfyui-feroccustomnodes
    

    Then restart ComfyUI. There are no dependencies and no model downloads - it's pure Python stdlib, so you don't even need to install requirements. It just works.

    Gotchas

    One real trap: since both sides get rounded to multiples of 8 independently, very extreme aspect ratios can drift a few pixels off your intended shape. If you're building a strict 16:9 or 9:16 pipeline and the node hands back 1216x680 instead of 1216x688, that's rounding, not a bug. And if you're on Flux, remember this guarantees multiples of 8, not 64 - double-check a divisibility node if you're picky. For everything else, set your megapixel target, drop the output into a latent, and stop doing resolution arithmetic in your head.

    CategoryFeroc

    Inputs (3)

    NameTypeDefaultDescription
    widthINT5121–1000000
    heightINT5121–1000000
    megapixelsFLOAT1.000.01–100

    Outputs (2)

    NameTypeDescription
    widthINT
    heightINT