Nodes/ComfyUI-Kaola-Nodes/Kaola Image Scale By Aspect Ratio
ComfyUI Node

Kaola Image Scale By Aspect Ratio

Crop, letterbox, or stretch to any ratio

By kana112233·Created 8 months ago·Updated 8 months ago· 0
Kaola Image Scale By Aspect Ratio
  • image
  • image
  • mask
  • original_size
  • target_size
aspect_ratiooriginal
aspect_ratio_width16.0
aspect_ratio_height9.0
scale_modeby_longest
target_length1024
fit_modeletterbox
background_color#000000
multiple_of8
methodlanczos

Diffusion models are picky about canvas shape. Feed a 1.78:1 image to an SDXL or Flux model trained on 1:1 and you're either wasting latent budget on a stretched composition or fighting letterbox bars you never asked for. KaolaImageScaleByAspectRatio is the "make it the right shape" step, and it does it without making you hand-calculate what 16:9 at a 1024 longest edge even comes out to.

It's the most featureful of the three nodes in the Kaola pack. It takes any image, decides a target aspect ratio and size, conforms it one of three ways - crop to fit, letterbox with bars, or stretch - and rounds the result to a multiple you choose. That last part matters more than it looks: latent diffusion is built around multiples of 8 (and often 64), so a clean 1024×576 instead of a weird 1023×574 is the difference between a smooth decode and artifacts on a batch.

The inputs that actually matter

There are nine, but you'll set three of them most of the time:

  • aspect_ratio - presets for the common shapes (1:1, 16:9, 4:3, 3:2, 2:3, 9:16, 3:4, 21:9, 9:21) plus original and custom. For custom, set aspect_ratio_width/aspect_ratio_height as floats.
  • scale_mode + target_length - this pair is where people mix things up. scale_mode decides which edge target_length pins: by_width, by_height, by_longest, or by_shortest. by_longest (the default) with 1024 means a landscape comes out 1024 wide and a portrait comes out 1024 tall.
  • fit_mode - letterbox (default), crop, or stretch. Stretch is usually the wrong answer for diffusion - it distorts geometry and faces. crop keeps the composition centered but throws away content. letterbox keeps everything and pads with background_color, which is the go-to for feeding a model a non-native aspect without distortion.

Rounding is multiple_of (default 8), and the resample algorithm is method - lanczos by default, which is the right default for both up and down.

The outputs

image, plus a mask and two size strings. The mask is the quiet win: in letterbox mode it's 1.0 over the real content and 0.0 in the bars, so you can composite the result back over other content later, or route it to a detailer/inpaint pass that should only touch the picture area. In crop and stretch modes the mask is just full white - don't go looking for a coverage mask there. original_size and target_size come out as plain "WxH" strings, handy for debugging and for display nodes.

How it works

Deterministic PIL math - no model, no VRAM, runs in milliseconds. It computes the target dimensions from the aspect ratio and scale mode, rounds down to multiple_of, then resizes each frame per the chosen method. It's the "reach for a millisecond, deterministic operation instead of burning a diffusion pass" category - it will never invent detail, and you wouldn't want it to.

Gotchas

  • multiple_of rounds down. Ask for by_width 1000 with multiple_of 8 and you get 992, not 1000. That's the feature working as intended, but it surprises people the first time.
  • The size strings describe the canvas, not the content. In letterbox mode, target_size is the full padded box, not the visible image.
  • Overlap with core ComfyUI. For plain resizing this duplicates ImageScale and ImageScaleToTotalPixels. Where it earns its place is the one-node combo - aspect ratio, fit mode, multiple-of rounding, and a mask - for prepping inputs to a generation pass.

Installation

Same as the rest of the pack, and it's light:

cd ComfyUI/custom_nodes/
git clone https://github.com/kana112233/ComfyUI-Kaola-Nodes.git

Restart after cloning, or use ComfyUI Manager (search "ComfyUI-Kaola-Nodes"). No models to download, no requirements.txt, no dependency drama - this is the rare custom-node install that just works.

For most people the combinations that actually get used are 16:9 or 9:16 + by_longest 1024 + crop for reframing, or letterbox for handing SDXL/Flux a non-native shape without distortion. Small pack, but this is the node you'd keep.

Categoryimage/transform

Inputs (10)

NameTypeDefaultDescription
imageIMAGE输入图像 | Input image
aspect_ratioCOMBOoriginal目标宽高比 | Target aspect ratio
aspect_ratio_widthFLOAT16.01–10000自定义宽高比宽度 (当选择custom时生效) | Custom aspect ratio width
aspect_ratio_heightFLOAT9.01–10000自定义宽高比高度 (当选择custom时生效) | Custom aspect ratio height
scale_modeCOMBOby_longest缩放基准模式 | Scale mode based on dimension
target_lengthINT102464–8192目标边长 | Target length for the chosen scale mode
fit_modeCOMBOletterbox适配模式: Letterbox(黑边), Crop(裁剪), Stretch(拉伸) | Fit mode
background_colorSTRING#000000Letterbox模式下的背景颜色(十六进制) | Background color for letterbox mode (hex)
multiple_ofINT81–512输出尺寸圆整倍数 (通常为8) | Round output dimensions to multiple of (usually 8)
methodCOMBOlanczos缩放算法 | Resampling method

Outputs (4)

NameTypeDescription
imageIMAGE
maskMASK
original_sizeSTRING
target_sizeSTRING