ComfyUI Node

Pt Pad

Pad an image tensor to a target size, centered, with black borders

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt Pad
  • tens
  • TENSOR
height0
width0

PtPad pads an image tensor up to a target height and width, centered, with black (zero) borders. It's the pack's answer to "I have a bunch of images of different sizes and I need them all to be the same shape before they go into a model" - the letterboxing step you'd normally do with PIL, done on raw tensors in the graph. This pack's image workflows are built on tensors, and PtPad is the shape-normalization tool that keeps them feeding into fixed-size models.

How it works

The node takes a tensor of shape (c, h, w) or (b, c, h, w), creates a new zero tensor of your target height × width, and centers the original inside it. Three inputs:

  • tens - the input tensor.
  • height / width - the target dimensions. The padding is centered: extra space is split between top/bottom and left/right.

Output is a single TENSOR of exactly (c, target_h, target_w) or (b, c, target_h, target_w).

The crucial gotcha: this is not a ComfyUI IMAGE

Here's where people lose an afternoon. ComfyUI's IMAGE type is (b, h, w, c) - channels last - but this node expects channels-first tensors. The pack's own docstring spells out the dance: to pad a ComfyUI image, first convert with Pt From Image, then use Pt Permute with (0, 2, 3, 1) to transpose (b, h, w, c)(b, c, h, w), then pad, then permute back with (0, 3, 1, 2) and convert with Pt To Image. Skip a step and you'll either get a shape error or, worse, a silently transposed image. There's also an explicit guard: if your target height/width is smaller than the input, the node raises a clear error rather than silently cropping - padding can only grow, which is the right behavior, just don't expect it to shrink.

When you'd use it

Batching images of different sizes for a training or inference run, or normalizing a single image to the exact resolution your model expects before prediction. Combined with Pt Permute, it's the standard prep pipeline between "image loaded in ComfyUI" and "tensor fed to a model node."

Installing it

Part of ComfyUI-Pt-Wrapper (HowToSD's no-code PyTorch pack, a spin-off of ComfyUI-Data-Analysis). ComfyUI Manager → search "ComfyUI-Pt-Wrapper", or:

cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper

Restart after; first boot is slow while pandas, scikit-learn, transformers, sentencepiece, peft and friends install. No model downloads needed.

CategoryData Analysis

Inputs (3)

NameTypeDefaultDescription
tensTENSOR
heightINT01–16384
widthINT01–16384

Outputs (1)

NameTypeDescription
TENSORTENSOR