Image Padding Calculator (rholdorf)
Padding math for outpainting, without the mental arithmetic
- image
- left
- top
- right
- bottom
Outpainting and canvas expansion have one fiddly bit that nobody enjoys: working out how much padding goes on each side. This node does that single calculation and hands you four integers. It's almost insultingly simple - which is exactly why you'll use it instead of doing division by hand at 11pm.
How it works
Give it an image plus target_width and target_height, and it computes the padding needed to center that image inside the target canvas. The rule that matters is pad = max(target − current, 0) on each axis, so an image already at or wider than the target returns zero padding on that axis rather than negative nonsense. Odd leftovers go to the right and bottom - left = pad_x // 2, right = pad_x − left - so the result stays centered to within a pixel, and the rounding lands on the side that matters less.
Inputs and outputs
Inputs: image (used only for its dimensions), target_width, target_height (both default 1024). Outputs: left, top, right, bottom as four INTs.
In a workflow
The node computes but doesn't apply - you wire the four outputs into something that pads. The obvious partner is a Pad Image node; for inpainting/outpainting, feed them into a crop-and-expand setup so the canvas grows to a target while your subject stays centered. It pairs naturally with the pack's Image Dimension Fitter: use the fitter to pick a model-native target size, then this node to work out how to center a non-matching image inside it without cropping away content.
There's no real trap here. The only thing to keep in mind is that "center the image" is the entire scope. If you want asymmetric padding - more space at the bottom, say, to extend a floor downward - this node isn't that; it's the center-case calculator, and you'd set target_width/target_height to the full canvas and let the leftovers fall where the math puts them.
Install
Part of the small comfyui_imgtools pack by rholdorf. Install once, get all 11 nodes:
cd ComfyUI/custom_nodes
git clone https://github.com/rholdorf/comfyui_imgtools
Restart ComfyUI. Or search "rholdorf" in ComfyUI Manager. No build step, no extra dependencies - pure Python on what ComfyUI already ships. Young, low-star solo pack: MIT, short readable code, no network calls, no model downloads. JS changes under web/ need only a browser refresh; Python changes need a restart.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| target_width | INT | 1024 | — |
| target_height | INT | 1024 | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| left | INT | — |
| top | INT | — |
| right | INT | — |
| bottom | INT | — |