ImageScaleToTotalPixels
Scale to a target megapixel count, rounded to a size the model likes
- image
- IMAGE
ComfyUI has a core node called ImageScaleToTotalPixels, and this is the "advanced" version of it: scale an image so it lands at roughly a target total pixel count - 1 megapixel, 2.25, whatever - rather than at a target side length. What makes it "Adv" is the second knob that core lacks: divisible_by, which rounds the final dimensions to a multiple you choose. That's the setting that saves you from the silent disaster of feeding a sampler a width it wasn't designed for.
The point of total-pixel scaling is simple. Models are trained on images of a certain total resolution, and upscaling by side length (512 → 1024) changes the total by 4x, which is how you accidentally OOM. Scaling to a megapixel target keeps the workload predictable: an 0.8 MP image goes to ~1.0 MP regardless of its aspect ratio. Set 1.0 for SDXL-class work, lower for video, and the node handles the math.
How it works
The math is one line: it computes a scale factor so that W*H*scale² equals megapixels * 1024², then rounds the new width and height up to the nearest divisible_by. Rounding means the actual result is usually slightly over the target - 1.0 MP with divisible_by = 8 can land at 1.04 MP. That's the trade for getting a model-friendly size, and it's the right trade. The resize itself goes through ComfyUI's common_upscale with your chosen method, plus an optional center crop if you set crop_method to center.
Inputs and outputs
image(IMAGE) - input, single or batch.upscale_method-nearest-exact,bilinear,area,bicubic,lanczos. For downscaling,areais the one that handles it properly (anti-aliased);lanczosfor quality upscales.megapixels(FLOAT, 0.01–64, default 1) - the target total.divisible_by(INT, 1–512, default 1) - round dimensions to a multiple. Set 8 or 16 for video/ViT-friendly sizes;1disables.crop_method-disabledorcenter.
One output: IMAGE, the resized result.
Installing it
Part of ComfyUI-lhyNodes:
- ComfyUI Manager → search
lhyNodes→ Install, restart. - Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/lihaoyun6/ComfyUI-lhyNodes.git
python -m pip install -r ComfyUI-lhyNodes/requirements.txt
Restart ComfyUI.
Where people get burned
The two most common mistakes are both defaults. upscale_method has no default in the schema, so it forces you to pick - and the first thing beginners pick is lanczos, which is heavy and ring-prone for a downscale; reach for area when shrinking. And divisible_by = 1 means no rounding, which quietly reintroduces the odd-sized-output problem this node is supposed to solve - if you're feeding a video model or a ViT-based model, set it to something meaningful like 16. The megapixel target is total, not per-side, so don't be surprised that a 2.25 MP setting doesn't produce a 1500×1500 square; it produces whatever keeps the aspect ratio at that total. For most workflows, set the MP and the divisor and forget it.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| upscale_method | COMBO | 5 options: nearest-exact, bilinear, area, bicubic, lanczos | |
| megapixels | FLOAT | 1.000.01–64 | — |
| divisible_by | INT | 11–512 | — |
| crop_method | COMBO | 2 options: disabled, center |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |