Image Square Padding Math
It doesn't pad the image — it tells you how much to pad, and that's more useful
- image
- left
- top
- right
- bottom
- width
- height
Some models want square input, and your images aren't square. The clean fix is to pad them to square before they go in - but how much to pad on each side is the annoying bit of math, especially when the difference is odd. This node does that math for you: give it an image, and it outputs the padding amounts (left, top, right, bottom) plus the original dimensions. Feed those numbers into an image-pad node and you get a centered, square image with no brain-work. It computes; you execute.
How it works
It reads the image tensor's height and width (from the [B, H, W, C] shape), then applies a simple rule:
- Landscape (
w > h): pad the top and bottom so the image becomes square.top = diff // 2,bottom = diff - top- and because it splits an odd remainder that way, odd differences get a one-pixel imbalance between top and bottom rather than a non-integer pad. - Portrait (
h > w): pad the left and right with the same even-split trick. - Already square: all four values come back
0.
So for a 1024×768 image you get left=0, top=128, right=0, bottom=128 - center the crop/pad and you've got 1024×1024. The width and height outputs return the original dimensions, which are handy if a downstream node needs them independently.
Inputs and outputs
image(IMAGE) - the image to measure.
Outputs (all INT): left, top, right, bottom - the padding amounts - plus width and height - the original dimensions.
Where it fits
Wire the four pad values into an image-padding/pad node (the same pack or any standard pad node) to square an image before a model that wants square input - a square-crop VAE, a tiling step, or a square-composition upscaler. Because it hands you the amounts as raw integers, you can also feed them into math nodes to, say, un-pad after processing, or use width/height for downstream layout math.
Install
Part of ComfyUI_EmAySee_CustomNodes. ComfyUI Manager → search "EmAySee" → install, restart. Or:
cd ComfyUI/custom_nodes
git clone https://github.com/EmAySee/ComfyUI_EmAySee_CustomNodes
No dependencies, no models - it's arithmetic on a tensor shape. Restart and you'll see the pack's "SPECTRE v5.0" banner in the console.
The honest take
The name says "Math" for a reason: it deliberately does not pad the image, and that separation is the right design. Image-pad nodes in ComfyUI expect explicit left/top/right/bottom values, so a node that hands you exactly those values plugs straight in. The one thing to remember is the odd-difference behavior - a 1001-pixel-wide image gets a 1-pixel asymmetry between sides. For most purposes that's invisible; if pixel-exact centering matters, do the // 2 split yourself and eat the extra pixel on a chosen side.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — |
Outputs (6)
| Name | Type | Description |
|---|---|---|
| left | INT | — |
| top | INT | — |
| right | INT | — |
| bottom | INT | — |
| width | INT | — |
| height | INT | — |