Nodes/TrentNodes/Just Pad or Crop It
ComfyUI Node

Just Pad or Crop It

Pad or crop an image to match a reference — without the distortion

By TrentHunter82·Created 9 months ago·Updated 4 days ago· 36
Just Pad or Crop It
  • reference
  • image
  • image
  • mask
gray_value0.50
alignmentcenter

You have an image that's 1024×1024 and a target frame that's 720×1280, and you need them to meet without smashing the square into a stretched mess. Just Pad or Crop It is the no-fuss answer: it takes your image and a reference image, and forces your image to the reference's exact dimensions - padding the too-small axes with gray, center-cropping the too-large ones.

The trick that makes it worth reaching for is that it handles each axis independently and, crucially, it does it without resampling. No blurry image.resize() squeeze. Small axes get solid gray bars, big axes get cropped, and the pixels that survive are untouched. That's the difference between "letterboxed" and "warped," and for anything where pixel integrity matters - feeding a reference image into a video model, building a contact sheet, aligning frames for a composite - the distinction is the whole point.

Inputs

  • reference - the image whose dimensions define the target size. Only its shape matters.
  • image - the image to pad or crop.
  • gray_value - brightness of the padding fill, 0.0–1.0. Default 0.5 is mid-gray, which is the right neutral for most diffusion inputs. Black (0) and white (1) both have uses.
  • alignment - center or top-left. This decides where the source image sits inside the target frame. Center is the default and usually what you want; top-left matters when you're tiling or you have an anchor you care about.

The mask output is the hidden feature

Alongside the padded image, this node emits a mask where 1.0 = real pixel, 0.0 = padded region. That's not an afterthought - it's what makes the node usable in an inpainting pipeline. You pad the image to fit the target, wire the mask into an inpainting sampler, and the model regenerates only the gray bars, giving you a full-bleed image that matches the target aspect without any upscaling artifacts. If your mask convention is inverted (you want 1.0 on the pads), the pack's companion node Just Mask Those Pads does exactly that.

Install

Ships in TrentNodes:

cd ComfyUI/custom_nodes
git clone https://github.com/TrentHunter82/TrentNodes.git
cd TrentNodes
pip install -r requirements.txt

Restart ComfyUI and it appears under Trent/Image. Pure torch tensor math, no model downloads, nothing GPU-heavy beyond the slice-assign it does.

Where people get burned

The alignment gotcha. If you pad with center here but some downstream node assumed top-left, the real pixels end up in a different spot than you think. Keep it consistent, and if you're pairing with the pack's Just Mask Those Pads, the tooltip there says it explicitly: the alignment must match what you used in Just Pad or Crop It, or your mask won't line up with your padding. It won't error - it'll silently be wrong, which is worse.

Also note the mask from this node is real-pixels = 1.0, which is the convention most ComfyUI composite nodes want. If your sampler wants the inverse, flip it or use the companion node. Ten seconds of confusion, fixed once.

CategoryTrent/Image

Inputs (4)

NameTypeDefaultDescription
referenceIMAGEReference image whose height and width define the target size
imageIMAGEImage to pad or crop to match the reference dimensions
gray_valueoptFLOAT0.500–1Brightness of the padding fill
alignmentoptCOMBOcenterWhere to anchor the source image within the target frame

Outputs (2)

NameTypeDescription
imageIMAGEPadded or cropped image matching reference dimensions
maskMASKBinary mask: 1.0 = real pixel, 0.0 = padded region