Nodes/Various ComfyUI Nodes by Type/Image Crop to Closest SDXL Resolution
ComfyUI Node

Image Crop to Closest SDXL Resolution

Snap any image to a valid SDXL size

By jamesWalker55·Created 3 years ago·Updated about a year ago· 215
Image Crop to Closest SDXL Resolution
  • image
  • IMAGE
  • WIDTH
  • HEIGHT
interpolation_mode

SDXL wasn't trained on arbitrary sizes. It learned on a fixed set of aspect-ratio "buckets" - 1024×1024, 1152×896, 896×1152, 1216×832, 832×1216, and so on, all landing near a megapixel of total area. Feed it something off-grid and you're more likely to get bad composition, duplicated subjects, or that stretched look. JWImageCropToClosestSDXLResolution takes any image, figures out which SDXL bucket best matches its shape, and crops it to fit - so your img2img or ControlNet input is on a resolution the model actually likes.

This is the node that quietly fixes a whole category of "why does my SDXL output look wrong" problems. You hand it a photo of unknown dimensions, it hands back one that lands on a supported bucket, no manual math about which resolution matches a 3:2 crop. Set it and forget it at the front of an SDXL pipeline.

How it works

It compares your image's aspect ratio to the SDXL bucket list, picks the closest one, and crops (not stretches) the image to reach those exact dimensions. Because it crops, some edge pixels get trimmed to hit the target shape - the image isn't distorted, it's tightened to the nearest valid ratio. It also reports the dimensions it chose, so downstream nodes can build a matching empty latent without you hardcoding numbers.

The inputs and outputs that matter

  • image (IMAGE) - the image to snap to an SDXL resolution.
  • interpolation_mode (enum: bicubic / bilinear / nearest / nearest exact) - how any resampling is done; bicubic is the safe default for photos.
  • Outputs:
    • IMAGE - the cropped, SDXL-valid image.
    • WIDTH (INT) and HEIGHT (INT) - the chosen dimensions. Wire these straight into an Empty Latent Image (or wherever the pipeline needs matching size) so the latent and the conditioning image always agree.

Those width/height outputs are the underrated part - they let the rest of the graph follow the crop automatically.

How to install it

Part of jamesWalker55/comfyui-various, a pack of small utility nodes.

  • ComfyUI Manager: search "Various ComfyUI Nodes by Type", install, restart. Red node in a downloaded workflow? Install Missing Custom Nodes.
  • Manually:
    cd ComfyUI/custom_nodes
    git clone https://github.com/jamesWalker55/comfyui-various
    
    Restart afterward. No models, no dependencies.

Under the jamesWalker55 category after restart.

Common issues

  • It cropped off part of my subject. Snapping to a bucket means trimming edges to reach the target ratio - if the subject was near an edge, it can get clipped. If you need everything preserved, resize-and-pad instead of crop, or compose with more margin before this node.
  • This is for SDXL - don't blindly use it on SD 1.5 or newer models. SD 1.5 buckets around 512, and newer base models (Flux, SD3-family) have their own preferred sizes. The buckets here are the SDXL set; on a different base model you may want plain resolution math instead.
  • Wire the width/height, don't retype them. If you hardcode the empty latent size and it doesn't match the crop, you'll get a resolution mismatch. Use the WIDTH/HEIGHT outputs so they stay in sync.
  • Node is red / missing. The pack isn't installed. Install via Manager or clone the repo, then restart.

For SDXL img2img on real-world photos, this is one of the genuinely handy nodes in the pack - it removes a fiddly manual step that's easy to get subtly wrong.

CategoryjamesWalker55

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
interpolation_modeCOMBO4 options: bicubic, bilinear, nearest, nearest exact

Outputs (3)

NameTypeDescription
IMAGEIMAGE
WIDTHINT
HEIGHTINT