Nodes/ComfyUI-A5Nodes/A5scale_to_total_pixels_safe
ComfyUI Node

A5scale_to_total_pixels_safe

Scale and letterbox in a single node

By A5Projects·Created 3 days ago·Updated 3 days ago· 1
A5scale_to_total_pixels_safe
  • image
  • padding_color
  • image
  • width
  • height
  • size_text
target_total_pixels786432
dimension_ruleMulti16
aspect_ratiodisabled
scale_policyboth
upscale_methodlanczos
width_override0
height_override0
keep_aspecton
aspect_handlingpadding
padding_sideright / bottom

This is the pack's do-everything resize node: hit a pixel budget, force an aspect ratio, and fill the leftover space with a colour instead of stretching your subject. The usual chain is a resize node, then an empty-latent node, then a pad or crop somewhere downstream - and three chances for the numbers to disagree. This collapses two of those steps.

The "safe" in the name isn't about padding, by the way. The author says it's ancient and refers to the multiples math.

What it does

Two jobs, in order.

First it resolves target dimensions, the same way the pack's scale-only node does: solve from target_total_pixels and the source aspect ratio, snap both sides to the dimension_rule (even through mul32), then apply scale_policy so you can forbid upscaling, downscaling, both, or neither.

Then, if you asked for a specific canvas - by picking an aspect_ratio, or by setting overrides with keep_aspect off - it either stretches the scaled image to that canvas, or pads it. Padding fits the image inside the canvas preserving its aspect (contain logic), fills the rest with padding_color, and puts the strip on one side: right / bottom, left / top, or center.

That's letterboxing rather than outpainting. Nothing gets generated into the empty band; you get a correctly proportioned canvas with your image sitting in it. Which is exactly what you want when a LoRA was trained on a particular aspect ratio, or when you're building a before/after strip, or when the model you're about to run genuinely prefers a shape your source isn't.

Inputs that matter

  • image
  • target_total_pixels - default 786432 (768×1024). The tooltip is the author's: 1000000 = 1 megapixel.
  • dimension_rule - even, Multi4, Multi8, Multi16 (default), Multi32, any.
  • aspect_ratio - nine options, disabled by default. Its tooltip says the quiet part out loud: enabling it activates padding.
  • scale_policy (both, only_downscale, only_upscale, none), upscale_method (lanczos default), width_override, height_override.
  • keep_aspect - with overrides, turning this off is what unlocks padding; the tooltip says it plainly.
  • aspect_handling - padding or stretch img. This is the fork in the road: pad, or distort.
  • padding_side - which side the band goes on. Centered is the safe default for framing; one-sided is what you want if the subject's already off-centre.
  • padding_color - hex, validated, picked off the canvas with the colour picker. The README admits the picker was a fight with browsers and Nodes 2.0, and that the dedicated button is the one that works everywhere.

Outputs are image, width, height, size_text - the numbers being the point, since you'll feed them to your latent node so the canvas and the latent agree.

Which of the three to use

  • only_scale - you just want a pixel budget. No canvas, no colour.
  • this one - you want a pixel budget and an exact aspect ratio or canvas, with the leftovers filled rather than cropped.
  • Pad Image for Outpaint - you want new pixels generated into the empty band, which needs a mask, and this node doesn't produce one.

Installing it

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

Restart ComfyUI and hard-refresh the browser. Or install A5 Custom Nodes through ComfyUI Manager - numbered release, not Nightly. No Python packages and no model downloads; it's a torch and ComfyUI-utilities node. Disable any older standalone copies of these nodes, since the pack keeps the original IDs on purpose and duplicates will complain.

Where people get burned

Padding didn't happen. Nothing to pad, essentially: it activates with an aspect ratio, or with overrides plus keep_aspect off, and not otherwise. Set the pixel target alone and you get a resize, silently, forever.

Both overrides plus keep_aspect on. The width wins and the height derives from the source ratio - your typed height is discarded. If you want the literal pair, keep_aspect off.

Stretch is not padding. aspect_handling on stretch img will happily squash a portrait into a square, and nothing warns you.

Solid-colour bands are a decision, not a default. A grey bar is honest; a sampled colour often isn't better. If you're compositing the result somewhere, matching the band to your target background - or colour-matching after the fact - beats guessing.

Your target is approximate. The multiple snapping means the final size is usually near your number rather than equal to it. size_text tells you what you actually got; read it once and stop wondering.

CategoryA5/image/transform

Inputs (12)

NameTypeDefaultDescription
imageIMAGE
target_total_pixelsINT7864324096–268435456Total pixels, 1000000=1 Megapixel
dimension_ruleCOMBOMulti166 options: even, Multi4, Multi8, Multi16, Multi32, any
aspect_ratioCOMBOdisabledEnabling activates padding
scale_policyCOMBOboth4 options: both, only_downscale, only_upscale, none
upscale_methodCOMBOlanczos5 options: lanczos, bicubic, bilinear, nearest-exact, area
width_overrideINT00–16384Set to 0 to disable. One value sets that dimension; odd values round up to even. The other follows the preset, input aspect, or input size. Both values set the canvas and disable automatic sizing.
height_overrideINT00–16384Set to 0 to disable. One value sets that dimension; odd values round up to even. The other follows the preset, input aspect, or input size. Both values set the canvas and disable automatic sizing.
keep_aspectCOMBOonDisabling+overrides activates padding
aspect_handlingCOMBOpadding2 options: padding, stretch img
padding_sideCOMBOright / bottom3 options: right / bottom, center, left / top
padding_colorCOLOR#808080

Outputs (4)

NameTypeDescription
imageIMAGE
widthINT
heightINT
size_textSTRING