Nodes/ComfyUi-Scale-Image-to-Total-Pixels-Advanced/Scale Image to Total Pixels Adv
ComfyUI Node

Scale Image to Total Pixels Adv

Resize to a pixel budget, not a dimension — and actually see what you got

By BigStationW·Created about a year ago·Updated 2 months ago· 42
Scale Image to Total Pixels Adv
  • image
  • image
  • width
  • height
megapixels1.05
multiple_of16
resize_modecrop
upscale_methodlanczos
width
height

You've been in this spot: the image needs to be about a megapixel because that's what the model was trained on, and you genuinely don't care whether that comes out as 1024×1024, 1152×896, or 1216×832 - you just want roughly a million total pixels, snapped to a multiple of 16 so the VAE doesn't smear the edges. That's the exact job this node exists for, and it's the version of the built-in one you'll actually want.

"Scale Image to Total Pixels Adv" (ImageScaleToTotalPixelsX) is BigStationW's take on ComfyUI's stock ImageScaleToTotalPixels - the resize node that appears in Comfy's own template workflows, from the Flux image-edit examples on down. The base node is serviceable but barely dressed: it resizes to a megapixel target and hands you the image. This one adds the three things people keep patching around it. It also outputs the width and height it actually chose, it snaps both dimensions to a multiple_of, and it lets you set megapixels to 0 to disable the pixel-budget math entirely and just do the snapping and crop/pad work.

How it works

Under the hood it's simple, reliable math. It reads the source size, computes scale_by = sqrt(target_pixels / current_pixels), and multiplies both dimensions by it - that's how it hits a pixel budget while keeping your aspect ratio. Then it snaps each side down to the nearest multiple_of and clamps so neither is ever smaller than that multiple. After that it's a plain resize (lanczos by default), followed by a center crop or a letterbox pad to hit the exact target box, depending on resize_mode. Nothing generative, nothing hallucinated - this is a "more pixels" node in the cleanest sense, the cheap rung of the upscaling ladder, and it runs in milliseconds.

Two details make it nicer than the stock node. The multiple_of snapping means you never feed an awkward size into a VAE again. And because it's marked an output node (OUTPUT_NODE = True), ComfyUI caches and broadcasts the final dimensions as text on the canvas - you see the real "W x H" it chose instead of guessing.

The inputs that matter

Of the six, you'll touch three:

  • megapixels - the target total, default 1.05. Set it to 0 to disable megapixel scaling and only snap/pad the source size.
  • multiple_of - the snapping factor, default 16. Use 8 for SD1.5-era latents, 16 for most modern VAEs, 64 for the models that demand it.
  • resize_mode - stretch distorts to the exact box, crop scales to cover and crops the center (default, usually what you want), pad scales to fit and letterboxes with black. The stock node only stretches; these two extra modes are worth the pack alone.

The optional width and height inputs are the manual override: wire both and they beat megapixels entirely - the node just resizes to those exact numbers. upscale_method (lanczos default) rarely needs touching.

What it outputs

Three things: the resized image, plus width and height as plain INTs. Those two numbers are the sleeper feature - feed them into anything downstream that needs to know the canvas it's working with, whether that's an Empty Latent sized to match or a text overlay. The image wires straight into your VAE encode, img2img, or the next stage of the graph.

Install

No dependencies, no model downloads, nothing to bake. It's a few hundred lines of plain torch using only what ComfyUI already ships. Either search ComfyUI Manager for "Scale Image to Total Pixels", or:

cd ComfyUI/custom_nodes
git clone https://github.com/BigStationW/ComfyUi-Scale-Image-to-Total-Pixels-Advanced

then restart ComfyUI. It appears as "Scale Image to Total Pixels Adv" under image/upscaling.

Where people get burned

  • If megapixels looks like it's being ignored, check the optional width/height inputs - a connected pair silently takes priority over the pixel budget.
  • The multiple_of clamp is a floor too. Even at megapixels = 0, a tiny source gets snapped up to at least multiple_of on each side, so don't expect a true pass-through.
  • It's an output node, so the image renders into the UI even without a Preview node wired in. Harmless mid-workflow, just don't be surprised by it.
  • One genuinely useful pattern: the "soft source" trick from Comfy Org's upscaling handbook. If an image is soft rather than small, downscale it to ~0.35 megapixels before a generative restore like SeedVR2 so the model rebuilds from a sharper-relative base. This node at megapixels = 0.35 is the clean way to do it - and it'll hand you the exact dimensions it chose on the way out.
Categoryimage/upscaling

Inputs (7)

NameTypeDefaultDescription
imageIMAGE
megapixelsFLOAT1.050–16
multiple_ofINT161–128
resize_modeCOMBOcrop3 options: stretch, crop, pad
upscale_methodCOMBOlanczos5 options: nearest-exact, bilinear, area, bicubic, lanczos
widthoptINT
heightoptINT

Outputs (3)

NameTypeDescription
imageIMAGE
widthINT
heightINT