Image Dimensions From Megapixels (CRT)
Size your latent to a pixel budget, not a guess
- image
- width
- height
Here's a workflow problem you've definitely had: you want to generate at a fixed pixel budget - say 1 megapixel - but you want to keep the aspect ratio of a source image, and the resolution has to land on a multiple the model likes (16 for most SD/SDXL-flavored models, 64 for LTX and Wan video). Do that in your head for a 4:5 portrait and you'll be rounding for a while. This node just does it: you feed it any image, it reads the aspect ratio, and outputs the width and height that hit your megapixel target while snapping to the nearest allowed multiple.
It's a two-integer utility node, and it's the difference between guessing "uh, 832×1216?" and getting exactly 0.99 MP on the right grid.
How it works
The math is straightforward: from the input image it derives the aspect ratio (w/h), computes dimensions that give you the requested pixel count at that ratio, then rounds both to the nearest multiple_of (default 16). Set multiple_of to 8 or 64 to match whatever the downstream model wants - snapping to the model's native grid matters more than hitting the megapixel target exactly, and the node prioritizes the grid.
Inputs and outputs
- image (
IMAGE) - the reference whose aspect ratio you're preserving. Any image works; it's only used for the ratio. - megapixels - target, 0.1–200 MP, default 1.0.
- multiple_of - the grid to snap to, default 16 (range 1–512).
Outputs are width and height (INT), which wire into an Empty Latent Image, an Empty SD3 Latent, a scale node, or any resolution-consumer. The common pattern: reference image → this node → Empty Latent Image → sampler, giving you generation at the source's aspect but a controlled pixel budget.
Installing it
CRT-Nodes, same as everything else: ComfyUI Manager → CRT-Nodes, or clone + pip install -r requirements.txt, restart. Under CRT/Image.
Where people get burned
It derives the ratio from the input image, not from a prompt or preset - if you feed it a 1:1 image and expect a 16:9 result, you'll get a square. And remember rounding is to the nearest multiple, so a 0.5 MP target on a 16-grid can land at 0.53 MP. That's fine - models care about the grid far more than the exact pixel count. One more: the pair of INT outputs is easy to swap accidentally if your target node expects height before width; check which order your downstream wants.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| megapixels | FLOAT | 1.000.1–200 | — |
| multiple_of | INT | 161–512 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| width | INT | — |
| height | INT | — |