Nodes/ComfyUI_AspectRatioToSize/CalculateImagePadding
ComfyUI Node

CalculateImagePadding

Exactly how much padding to letterbox an image into a ratio

By sugarkwork·Created 2 years ago·Updated 2 months ago· 3
CalculateImagePadding
  • image
  • left
  • right
  • top
  • bottom
aspect_ratio16:9

"How much do I pad this thing?"

You have an image that isn't the ratio you need - too wide, or too tall - and you want to pad it into the right shape without cropping. The fiddly part isn't the padding itself; it's knowing how much to add on each side so the final canvas hits the target ratio. That's CalculateImagePadding's one job: look at your image, look at the ratio you want, and hand back four integers - left, right, top, bottom - telling you exactly what to add.

What it does (and doesn't do)

This matters: it doesn't pad anything. It only computes the amounts. You feed the four ints into a padding node yourself (ComfyUI's ImagePad or whichever pack you use). CalculateImagePadding is a calculator, not a transformer.

The math behind it: it reads the image's width and height, works out the current ratio, and compares it to the target ratio from your aspect_ratio string. Three cases:

  • Ratios match → (0, 0, 0, 0). Nothing to do.
  • Image is wider than the target → the canvas needs height, so it pads top and bottom, split evenly.
  • Image is taller than the target → it pads left and right, split evenly.

Note it only ever pads one axis. That's the difference between "add exactly enough to reach the ratio" and a general letterbox; if you want padding on all four sides for aesthetic reasons, this isn't that node - add the extra yourself.

Inputs and outputs

  • image - any IMAGE tensor, straight from a Load Image or anything upstream.
  • aspect_ratio - a "16:9"-style string, default 16:9.

Outputs are the four INT padding values in pixels: left, right, top, bottom. When an amount is odd, the code floors the half and gives the remainder to the second side, so you may see top=27, bottom=28 - that's expected, not a bug.

Where it fits

This is a compositing and img2img helper. Common spot: you've got a source image with an awkward aspect and you want to generate at that ratio without the model stretching it - match the ratio with MatchImageToAspectRatio, pad the image to it with this node, then generate. It's also handy before outpainting or background-extension workflows where you want the canvas shape decided for you.

One thing to check after you pad: the node doesn't round the result to a multiple of 8 or 64, and padding an off-multiple image can leave a canvas a strict VAE or Flux won't accept. If generation errors, bump the final size to the next multiple of 8 (or 64 for Flux) in your pad node.

Install

It's in the same single-repo pack:

cd ComfyUI/custom_nodes
git clone https://github.com/sugarkwork/ComfyUI_AspectRatioToSize

Restart ComfyUI - or use ComfyUI Manager and search "AspectRatioToSize". Zero dependencies, zero model downloads.

Categoryimage

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
aspect_ratioSTRING16:9

Outputs (4)

NameTypeDescription
leftINT
rightINT
topINT
bottomINT