Nodes/ComfyUI-A5Nodes/A5only_scale_to_total_pixels
ComfyUI Node

A5only_scale_to_total_pixels

Think in megapixels, not in width and height

By A5Projects·Created 3 days ago·Updated 3 days ago· 1
A5only_scale_to_total_pixels
  • image
  • image
  • width
  • height
  • size_text
target_total_pixels786432
dimension_rulemul16
scale_policyboth
upscale_methodlanczos
width_override0
height_override0
keep_aspecton

A resize node that takes a pixel budget instead of a width and a height. You tell it "about 1 megapixel" and it works out the rest from whatever aspect ratio came in - which is the correct way to size an image for a diffusion model, because models care about total pixels far more than they care about sides.

It's the plainest of the pack's three resize nodes: no padding, no aspect-ratio presets. If you want letterboxing, you want its siblings instead.

Why total pixels

Checkpoints have a native resolution band, and it's a budget, not a shape. SDXL is happiest around 1024×1024 - about 1MP. Z-Image degrades past roughly 2MP. Flux 2 goes to 4MP if your card can stand it, and HiDream-O1 is baked to 2048×2048 whether you like it or not. Meanwhile the same 1MP can be 1024×1024, 1344×768, or 1536×640 depending on the aspect you need.

So "scale this to one megapixel, keep my aspect, snap the sides to multiples of 16" is a much more useful instruction than typing numbers. That's the node, and the "safe" in the pack's naming - the author says it's ancient and just means the multiples math - started here.

How it works

It solves the geometry first, then resizes once.

Given the source aspect ratio and the target, it computes an ideal width as sqrt(target_pixels * ratio), then searches a window of nearby widths (about ±64 pixels) and picks the pair whose area is closest to your target and whose aspect is closest to the source. Every candidate gets snapped to the multiple you chose.

Then the scale policy decides whether that result is allowed to happen. only_downscale clamps the target to your current pixel count if you asked to grow; only_upscale does the mirror; both just does what you asked; and none clamps either direction - which leaves you with a node that re-snaps your existing size to the multiple rule without rescaling anything. That last one is genuinely useful before a latent round-trip: get everything onto multiples of 16 without touching the pixels.

Lanczos isn't a torch interpolate mode the way the others are - the node routes it through ComfyUI's own comfy.utils.lanczos, and sends bicubic/bilinear/nearest-exact/area to torch.nn.functional.interpolate. In practice: lanczos is the sane default for a downscale before generation, area is the classic clean downscaler, nearest-exact is for pixel art and masks.

Inputs and outputs

  • image - batch-aware; the whole batch is resized to the same target.
  • target_total_pixels - default 786432, which is 768×1024. One megapixel is 1000000.
  • dimension_rule - even, mul4, mul8, mul16 (default), mul32, any.
  • scale_policy and upscale_method as above.
  • width_override / height_override - 0 means off. One value, with keep_aspect on, sets that side and derives the other from the source ratio. Both values with keep_aspect on is the trap: the width wins and the height is derived, so your height override gets overwritten.
  • keep_aspect - turn it off and the overrides become literal dimensions; missing sides fall back to the source size. Unlocking aspect means the image stretches.

Outputs are image, plus width, height and size_text as INT/INT/STRING. Wire the integers into anything else that needs the same numbers - a latent node, a crop, a mask - and stop hand-syncing them.

Installing it

cd ComfyUI/custom_nodes
git clone https://github.com/A5Projects/ComfyUI-A5Nodes

Then restart ComfyUI and refresh the browser. ComfyUI Manager lists it as A5 Custom Nodes (choose the numbered version, not Nightly). No dependencies, no models to download. If you still have the older standalone copies of these nodes, disable them - the pack preserves the original node IDs specifically so old workflows keep working, and duplicates will fight.

Where people get burned

The target is approximate. Snapping to multiples means the node often can't hit your number exactly; it gets closest. If you need exactly 1024×1024, use overrides, and expect the rule to round them anyway.

any is a footgun for latents. Most latent grids want multiples of 8 or 16. Use any on images you're exporting or compositing, not on ones headed into a VAE.

Overrides ignore the policy. If you type a width and height, the scale policy and target pixels stop mattering. That's intended, but it surprises people who set only_downscale and then watched their image get bigger.

A downscale before an upscale is not a mistake. If your source is soft rather than small, dropping it to a fraction of a megapixel before a SeedVR2 or tiled pass gives the model a smaller, sharper-relative base to rebuild from. This node is the first half of that pipeline, and its size_text output is a cheap way to confirm what actually happened.

CategoryA5/image/transform

Inputs (8)

NameTypeDefaultDescription
imageIMAGE
target_total_pixelsINT7864324096–268435456
dimension_ruleCOMBOmul166 options: even, mul4, mul8, mul16, mul32, any
scale_policyCOMBOboth4 options: both, only_downscale, only_upscale, none
upscale_methodCOMBOlanczos5 options: lanczos, bicubic, bilinear, nearest-exact, area
width_overrideINT00–16384
height_overrideINT00–16384
keep_aspectCOMBOon2 options: on, off

Outputs (4)

NameTypeDescription
imageIMAGE
widthINT
heightINT
size_textSTRING