Nodes/comfyui-agent-adapter/Calculate Image Size
ComfyUI Node

Calculate Image Size

Stop doing aspect-ratio math by hand — let this node do it

By yaofeng·Created 6 months ago·Updated 6 months ago· 0
Calculate Image Size
  • image
  • new_width
  • new_height
  • pad_top
  • pad_bottom
  • pad_left
  • pad_right
aspect_ratio1:1
auto_fliptrue
horizontal_aligncenter
vertical_aligncenter

Here's a workflow moment you've definitely had: you're feeding an image into something that demands a specific aspect ratio - a video model like LTX or Wan that wants 16:9, a canvas node that wants a phone-portrait 9:19, an upscaler that wants a clean target size - and you're sat there with a calculator trying to figure out what dimensions to pad to. ImageSizeCalc is the node that does that arithmetic for you, and only the arithmetic. It's the odd one out in the comfyui-agent-adapter pack: no Base64 anywhere, just six integers out. It doesn't touch a single pixel - it tells you the numbers, and you (or another node) do the actual padding.

Why is it in an "agent adapter" pack? Because when an agent hands you an arbitrary image, you rarely know its dimensions in advance, and your workflow needs to adapt at runtime. A hard-coded "pad to 1280x720" breaks the moment the agent sends something portrait. This node reads the incoming image's actual size and computes the pad values on the fly.

How it works

The logic is simple and worth knowing because it explains the outputs: it picks a target aspect ratio, then keeps the image fully intact and extends one dimension - it never crops and never resizes. If your image is wider than the target ratio, it keeps the width and grows the height; if it's taller, it keeps the height and grows the width. The difference between the new size and the original becomes padding, distributed according to your alignment. So the whole thing is: fit the image into the ratio by letterboxing, then hand you the exact pixel values.

The auto_flip toggle is the bit people underestimate. It's on by default, and it matches orientation: a portrait image against a "16:9" target automatically flips the ratio to 9:16 so the image's long edge maps to the ratio's long edge. Turn it off and a portrait image gets weirdly squashed into a landscape frame. Leave it on unless you have a reason not to.

Inputs and outputs that matter

  • image - the tensor whose dimensions you're calculating from. This is a required input, so the node can't run standalone; give it any IMAGE upstream.
  • aspect_ratio - one of nine presets: 1:1, 4:3, 3:2, 16:9, 21:9, 9:16, 3:4, 2:3, 9:19. The last one is phone-vertical; 21:9 is ultrawide. No custom ratios, so if you need something odd you're out of luck.
  • horizontal_align / vertical_align - left / center / right and top / center / bottom. Center is the default and usually right; the anchors matter when you want the image pinned to one edge (say, keeping the subject's position predictable before an outpainting or inpainting pass).

Outputs are six INTs: new_width, new_height, pad_top, pad_bottom, pad_left, pad_right. You wire those into whatever actually does the padding - a pad/canvas node or a resize-to-size node. The pack doesn't ship that padding node, which is the gotcha: ImageSizeCalc is a calculator, not a doer. If you feed its outputs into nothing, nothing happens. That's by design (it's a pure helper), but it trips people up the first time.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/yaofeng/comfyui-agent-adapter
cd comfyui-agent-adapter
pip install -r requirements.txt

then restart ComfyUI, or find the pack in ComfyUI Manager. Requirements are Pillow, torch, numpy - all stock - and there are no models to fetch. The whole pack is a single commit from a brand-new repo, and honestly this node is the least "agent-y" of the three: if you only need aspect-ratio math, plenty of packs have equivalent calculators with battle-tested padding nodes attached. This one is worth grabbing mainly because it's already on your machine once you've installed the pack for the Base64 pair.

Categoryagent_adapter

Inputs (5)

NameTypeDefaultDescription
imageIMAGE
aspect_ratioCOMBO1:19 options: 1:1, 4:3, 3:2, 16:9, 21:9, 9:16, +3
auto_flipBOOLEANtrue
horizontal_alignCOMBOcenter3 options: left, center, right
vertical_alignCOMBOcenter3 options: top, center, bottom

Outputs (6)

NameTypeDescription
new_widthINT
new_heightINT
pad_topINT
pad_bottomINT
pad_leftINT
pad_rightINT