ImageNormalization-badger
Fit any image into any canvas without distortion — the padding math, pre-computed
- new_width
- new_height
- top
- left
- bottom
- right
"Fit this image into a 1024×1024 canvas without stretching it" is a question ComfyUI asks you constantly - training batches want uniform sizes, video standards want fixed resolutions, compositors want a target canvas. And the honest answer is annoying: you scale one dimension, then you pad the other, and you have to compute both by hand depending on which orientation your image is in. ImageNormalization-badger does that whole calculation in one node and hands back the six numbers you need: the new size plus the padding on every side.
It's a pure-math node - no pixels are touched. It just tells you what to do. You feed it the current width/height and the target width/height, and it returns the fitted dimensions and the exact padding required, centered. Then you wire those numbers into a scale node and ExpandImageWithColor-badger (same pack) and you've got a distortion-free fit-to-canvas with zero manual aspect-ratio arithmetic.
How it works
It compares two aspect ratios - yours and the target's. If your image is wider than the target ratio, it scales to the target width and pads top and bottom; if it's taller, it scales to the target height and pads left and right. Padding is split in half and rounded (one side gets the extra pixel when odd), so the result is centered rather than shoved into a corner. That "which axis do I pad?" decision is the whole subtlety, and it's exactly what people get wrong when doing this by hand.
Inputs and outputs
- width / height - the current image dimensions.
- target_width / target_height - the canvas you want to fit into.
Six outputs, all INTs:
- new_width / new_height - the scaled size that preserves your aspect ratio.
- top / left / bottom / right - the padding in pixels on each side to reach the target canvas, centered.
The pattern: use getImageSide + getImageSize-style nodes or just type the current size in, then feed new_width/new_height to a resize node and the four padding values to ExpandImageWithColor.
Installing it
In ComfyUI_BadgerTools: ComfyUI Manager → search "BadgerTools" → install, or
cd ComfyUI/custom_nodes
git clone https://github.com/AbyssYuan0/ComfyUI_BadgerTools
Restart after install. No models. Pack tax: heavyweight requirements import at startup even for this pure-integer-arithmetic node.
Gotchas
- It computes; it doesn't execute. Nothing here resizes or pads anything. If you wire only this node's outputs into a display you'll see numbers, not a fitted image. You must feed the outputs into actual resize/pad nodes.
- The schema defaults are all 1.0, which means the node's widgets start in a nonsense state - an empty or defaulted run gives you "fit 1×1 into 1×1," i.e. all zeros. Set real numbers before running.
- Odd dimensions round up on one side. You may see top/bottom differ by one pixel; that's the centering math, not a bug.
The KB's upscaling essay keeps reminding people that "which job are you doing?" comes before "which settings?" - and "fit without distortion" is its own little job. This node is the calculation half of it, packaged so you don't have to reach for a calculator every time your images refuse to match your canvas.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| width | INT | 10–4096 | — |
| height | INT | 10–4096 | — |
| target_width | INT | 10–4096 | — |
| target_height | INT | 10–4096 | — |
Outputs (6)
| Name | Type | Description |
|---|---|---|
| new_width | INT | — |
| new_height | INT | — |
| top | INT | — |
| left | INT | — |
| bottom | INT | — |
| right | INT | — |