Nodes/comfyui-find-perfect-resolution/Find Perfect Resolution
ComfyUI Node

Find Perfect Resolution

It does your aspect-ratio math so your latent sizes stop erroring

By ashtar1984·Created 11 months ago·Updated 9 months ago· 25
Find Perfect Resolution
  • image
  • mask
  • width
  • height
  • IMAGE
  • mask_output
  • resolution_info
desired_width0
desired_height0
divisible_by16
upscalefalse
upscale_methodlanczos
small_image_modenone
pad_color#000000

Every ComfyUI user has had this moment: you want to feed a 4:5 photo into a workflow at roughly a megapixel, keep the aspect ratio, and stay on a size the VAE actually accepts - so you fire up a calculator, do the multiplication, and round by hand. Find Perfect Resolution is that calculation, packaged as a node. Give it an image and a target ballpark, and it hands back a width and height that preserve the ratio, hit close to your target pixel count, and land exactly on a value you specify.

How it works

The logic is refreshingly transparent. It reads the source image's actual dimensions, then treats desired_width × desired_height as a target pixel budget. From there it's one formula: h = round(sqrt((target_pixels × orig_h) / orig_w) / divisible_by) * divisible_by, and width follows from the aspect ratio. Set one of the desired dimensions to 0 and it derives it from the other using the image's real ratio - handy when you only care about one side. Set both to 0 and it raises an error, which is its way of telling you it can't guess the aspect ratio from nothing.

Beyond the numbers, it can also resize the image itself using Pillow (lanczos by default). Nice touch for a utility: it shows the computed result live under the node as you drag the sliders.

The inputs that matter

Only a few of these do you actually touch:

  • image (required) - anything from a Load Image node. Its aspect ratio is the thing being preserved.
  • desired_width / desired_height - the target. 0 means "derive from the other one." Note the code's default is 0 (auto), not the 512 the README still claims; source wins.
  • divisible_by - the grid you snap to. Default 16, which is right for most SD/SDXL work. If you're feeding a Flux model, bump it to 64 - Flux wants multiples of 64, and this node will happily enforce that so you never hit the mismatch errors downstream.

The rest are optional and mostly self-explanatory: upscale (off by default), upscale_method, small_image_mode for tiny inputs (crop or pad, with pad_color to set the fill), and a mask input so an inpaint mask gets the exact same treatment.

The outputs

You get five. The two that matter are width and height (both INT) - wire those into a Resize Image v2, an Empty Latent, or a Latent Upscale and your workflow is automatically on-grid. There's also the resized IMAGE (kept at its original size if the target would be an upscale and upscale is off - you'll still get the numbers), a mask_output resampled with nearest-neighbor so hard mask edges stay sharp, and a resolution_info string like 1024x1344 | 4.11MB | 1,376,256 pixels if you want the stats in a text box.

Installing and a word of caution

Install is as painless as it gets: in ComfyUI Manager, search for comfyui-find-perfect-resolution, or

cd ComfyUI/custom_nodes
git clone https://github.com/ashtar1984/comfyui-find-perfect-resolution

then restart. There are no model downloads and no extra pip dependencies beyond torch and Pillow, which ComfyUI already ships. Be honest about what this is, though: the pack is brand new (a single commit) with basically no community footprint, so don't expect a support forum. The formula itself is dead simple and verifiable - that's the whole appeal. If you're sick of eyeballing resolutions and getting "latent shape mismatch" for it, this is a fine fix; if you just resize by hand occasionally, it's a nice-to-have, not a must-have.

Categoryutils

Inputs (9)

NameTypeDefaultDescription
imageIMAGE
desired_widthINT00–8192
desired_heightINT00–8192
divisible_byINT161–128
maskoptMASK
upscaleoptBOOLEANfalse
upscale_methodoptCOMBOlanczos4 options: lanczos, bilinear, bicubic, nearest
small_image_modeoptCOMBOnone3 options: none, crop, pad
pad_coloroptSTRING#000000

Outputs (5)

NameTypeDescription
widthINT
heightINT
IMAGEIMAGE
mask_outputMASK
resolution_infoSTRING