Nodes/ComfyUI-AI-Assistant/SDXL Image Ratio
ComfyUI Node

SDXL Image Ratio

Stop Guessing SDXL Resolutions — Let It Read the Aspect Ratio for You

By JackEllie·Created 2 years ago·Updated 2 years ago· 24
SDXL Image Ratio
  • image
  • Target Width
  • Target Height
  • show_help

SDXL is trained at 1024px-ish, but not at one resolution - it was trained on a ladder of aspect ratios, and if you generate at a ratio it wasn't trained on, anatomy stretches and composition falls apart. The classic beginner trap is feeding a 512px-era image into an SDXL workflow and getting a wobbly result, or manually typing 1344x768 when your input is actually 16:10. SDXL Image Ratio (that's the display name for resize_image_sdxl_ratio) removes the guesswork: you feed it an image, it figures out the closest SDXL-native aspect ratio, and hands you the target width and height as plain integers.

How it works

The mechanism is refreshingly simple. The node reads your image's dimensions, computes the aspect ratio (width / height), then compares it against a hard-coded table of standard ratios and returns the pair whose ratio is closest:

  • 1:1 → 1024x1024
  • 4:3 → 1152x896, 3:2 → 1216x832, 16:9 → 1344x768
  • 21:9 → 1568x672, 3:1 → 1728x576
  • and the portrait rotations: 3:4 → 896x1152, 2:3 → 832x1216, 9:16 → 768x1344, 1:3 → 576x1728, 1:4 → 512x2048

Worth knowing: the first five are the real SDXL training buckets, but 1568x672, 1728x576 and 512x2048 are not SDXL-native - the author added them for very wide/tall crops. So "closest SDXL ratio" is doing some work there; if you feed it a 21:9 panorama you'll get a size SDXL was never trained on, and your mileage may vary. For the sane 1:1 to 16:9 range it's spot-on.

The inputs and outputs that matter

The only input is image - anything an image loader gives you. It doesn't resize anything itself; it's a measurer. The two outputs that matter are:

  • Target Width (INT)
  • Target Height (INT)

Wire those into the width and height inputs of a standard ImageScale node and - this is the part people miss - enable crop on that ImageScale. The author's own description says to use it with ImageScale's crop function turned on, because you want to crop to the target ratio, not squash the image into it. The third output, show_help, is just a text reminder of that same instruction.

Installing it

It lives in the JackEllie/ComfyUI_AI_Assistant pack, a small fork of tori29umai0123's AI-Assistant. Install via ComfyUI Manager (search "ComfyUI-AI-Assistant") or:

cd ComfyUI/custom_nodes
git clone https://github.com/JackEllie/ComfyUI_AI_Assistant

Then restart. No model downloads, nothing heavy - it's pure arithmetic on the image shape. The pack's only declared dependency is opencv-python, though it imports skimage at module load, so if the whole pack errors out with ModuleNotFoundError: skimage, install scikit-image.

Gotchas

The big one is remembering crop mode on the downstream ImageScale - without it you'll stretch the image to the target box and the aspect ratio fix is pointless. Also note the matching rounds to the nearest ratio key, so a 1.45 image lands on 4:3 (1152x896) rather than exactly 3:2; that's normal, it's what "nearest bucket" means. And while this node is written for SDXL, you can reuse it for SD 1.5-era workflows by just accepting the resolution it hands you and downscaling - the ratio math doesn't care about the model. It's a tiny tool, but it kills the most common source of stretched generations.

Categoryadvanced/AI_Assistant/image

Inputs (1)

NameTypeDefaultDescription
imageIMAGE

Outputs (3)

NameTypeDescription
Target WidthINT
Target HeightINT
show_helpSTRING