Nodes/comfyui-common-util/Image Resize With Padding
ComfyUI Node

Image Resize With Padding

Fit an image to a canvas without cropping — that's the whole trick

By longgui0318·Created 2 years ago·Updated about a year ago· 1
Image Resize With Padding
  • image
  • IMAGE
width0
height0

"Image Resize With Padding" is a "fit, don't crop" resize. You give it a target width and height, it scales your image up or down to fit inside that box while keeping the aspect ratio, then pads the leftover space with black so the output is exactly the size you asked for. Think CSS object-fit: contain - letterbox bars included.

Why bother? Plenty of ComfyUI nodes are picky about input dimensions - latents, batches, anything you're compositing onto a fixed canvas, ControlNet preprocessors. The stock ImageScale node just stretches or needs you to hand-roll the aspect-ratio math. This one gives you "fit + letterbox" in a single node, which is exactly what you want when you're normalizing a pile of differently-shaped images down to one canvas before feeding them into the same pipeline.

How it works

Under the hood it's simple and predictable. It computes a scale factor from the two target values, resizes with Lanczos for quality, then pads with constant (black) pixels - evenly split between left/right and top/bottom - until the image hits the exact target size. One nice touch: if the input is RGBA it resizes the RGB and alpha channels separately, so transparency survives the trip. That matters if you're feeding it cutouts.

The inputs that matter

  • image - your input.
  • width, height (INT, both default 0) - the target box. The author's tooltip says it right: it won't crop, it scales proportionally and then pads.

Where people get burned

The defaults are 0 and 0, and setting either to zero crashes the node - OpenCV throws an inv_scale_x > 0 assertion error the moment the resize hits a zero dimension. This is easily the most common failure here, and the error message reads like a geometry problem instead of a missing value. Fix: always set both width and height to real numbers.

Second gotcha: the padding is always black, and there's no option to change it in this node. If you need white or a color background, composite the result over a solid-color canvas yourself. And note the padding is constant, not mirrored or edge-replicated - usually fine for display, but if you're feeding padded images into a sampler the hard black bars can occasionally show up in results.

Installing it

The pack is longgui0318/comfyui-common-util:

cd ComfyUI/custom_nodes
git clone https://github.com/longgui0318/comfyui-common-util
cd comfyui-common-util
pip install -r requirements.txt

Restart ComfyUI after. Or just search "comfyui-common-util" in ComfyUI Manager and click install - same result, no terminal. The README is basically one line, and requirements.txt only lists opencv-python, but the code also imports scipy, scikit-image, and PyWavelets at load time, so if the pack refuses to load with a ModuleNotFoundError, pip install scipy scikit-image PyWavelets fixes it.

Output

Single IMAGE output at exactly width×height. Wire it straight into whatever demanded the fixed canvas - that's the whole point of the node.

Categoryutils

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
widthINT0Target width. Note that it will not crop the original image, but will scale proportionally based on width and height, then pad
heightINT0Target height. Note that it will not crop the original image, but will scale proportionally based on width and height, then pad

Outputs (1)

NameTypeDescription
IMAGEIMAGE