SizeApproximation
A pixel budget for img2img
- image
- TUPLE
- INT
- INT
The time an img2img run takes tracks total pixels more than it tracks width and height. Feed it a 512×512 image one run and a 1792×1024 the next, and your generation times swing wildly for no good reason. SizeApproximation's job is to compute a size that keeps your image's aspect ratio but lands near a target pixel count - and to hand it to the next node on a single wire.
It's one of the ten nodes in the small ComfyUI-TeaNodes pack by TeaCrab, MIT, in the TeaNodes/Image menu. The author's own README: "This algorithm is really dumbly written, but it works." Accurate on both counts.
How it works
You give it an image and a square value (default 1024). It computes square × square as the target pixel budget (roughly a megapixel at the default), then hunts for a width/height pair that keeps your ratio and gets within a tolerance of that budget. The implementation floors the longer side down to a power of two first, then steps toward the target in progressively finer increments, with a debounce set so it can't loop forever. Output lands within about ±12.5% of the budget - close enough that generation time is stable, far enough that the node stays fast.
That's the whole trick: it's not about quality, it's about a consistent cost per run. README says it best - "great for stabilizing the speed of image-to-image generation."
The inputs and outputs that matter
image- the image whose ratio you're keeping (any IMAGE).square- the per-side target,INT512–4096 in steps of 32. 1024 = ~1MP budget.
Outputs are the fun part, because it gives you three views of the same answer:
TUPLE-(width, height), the one you wire into TC_ImageResize'ssizeinput. That pair is made for each other.INTandINT- width and height individually, for nodes that want plain integers (or for display).
The classic workflow
Load Image → SizeApproximation ──TUPLE──> TC_ImageResize ──> VAEEncode ──> sampler (img2img)
Every source image gets resized to roughly the same pixel count before it hits the sampler, so run time stays flat and the sampler isn't choking on an 8MP input one run and loafing on a 0.25MP one the next. Crank square down for quick preview batches, up for final passes.
Where it bites
It keeps the input's ratio, so a 16:9 photo gives you a 16:9 result - if you wanted a square, resize or crop first. And because the output is approximate, don't use it where a node needs an exact multiple-of-64 latent size downstream; that's what crop or the resize's own step-32 widgets are for.
Installing it
ComfyUI Manager → search ComfyUI-TeaNodes, or:
cd ComfyUI/custom_nodes
git clone https://github.com/TeaCrab/ComfyUI-TeaNodes
restart ComfyUI. No models, no heavy dependencies - the pack's only declared requirement is the author's regex_spm helper.
If your img2img runs feel like a dice roll on time, this is the stabilizer. Dumb algorithm, smart outcome.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| square | INT | 1024512–4096 | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| TUPLE | TUPLE | — |
| INT | INT | — |
| INT | INT | — |