Nodes/ComfyUI-XJNodes/Image Scale Megapixel
ComfyUI Node

Image Scale Megapixel

Target a megapixel count, not a lucky scale factor

By alexjx·Created 10 months ago·Updated 4 months ago· 0
Image Scale Megapixel
  • image
  • image
  • width
  • height
megapixels1.00
round_to8
methodfit
upscale_modelanczos

Every image-processing workflow eventually hits the same question: "how big do I want this?" If your honest answer is "about a megapixel, whatever that works out to in pixels," then this is the node you've been missing. Image Scale Megapixel resizes an image to a target megapixel count - not a scale factor, not a fixed resolution, a pixel budget. It's the right tool whenever consistency of total resolution matters more than the exact dimensions.

The obvious use is normalizing a pile of mixed-resolution images before they hit a model that cares about total pixels. Batch up 600×600, 1216×832, and 768×1024 reference shots, tell this node "1.0 megapixel," and out comes a set of images that all occupy roughly the same pixel budget. That's the kind of thing training-script authors hand-roll with math; here it's a node with three useful settings.

How it works

Under the hood it's straightforward: current width × height gives you the starting pixel count, the target is megapixels × 1,000,000, and the node scales the image by the square root of the ratio. The interesting part is the method dropdown, which changes what "reach the target" means:

  • fit (default) - maintain aspect ratio, scale so the image fits within the target budget. This is the safe one.
  • fill - scale to hit the target regardless of aspect ratio. Your 16:9 image comes back square-ish. Handy if you genuinely don't care about shape, surprising if you do.
  • crop - scale up to cover the target and center-crop the overflow. Keeps aspect ratio, changes composition. Good for filling a fixed frame.

Two more inputs do the fine-tuning: round_to snaps both dimensions to a multiple (8, 16, 32, 64, 128 - default 8), and upscale_mode picks the resampling filter (lanczos, bicubic, bilinear, nearest-exact, area). Because the final size is rounded, the actual pixel count lands near the target rather than exactly on it - the rounding is what makes the output usable in a graph.

Inputs and outputs

The important ones:

  • image - the IMAGE you're resizing
  • megapixels - FLOAT, 0.01 to 16, default 1.0. Your pixel budget
  • method - fill / fit / crop, default fit
  • round_to and upscale_mode - leave them alone until you have a reason not to

Outputs are image (the resized result), plus width and height as INTs so you can wire the real dimensions onward without asking another node to guess.

Installing it

Part of the XJNodes pack, so it installs with everything else. In ComfyUI Manager search "ComfyUI-XJNodes", install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/alexjx/ComfyUI-XJNodes
# restart ComfyUI

No extra dependencies - torch, numpy, and Pillow cover it, and the resizing itself delegates to ComfyUI's built-in common_upscale. Look for it under XJNodes/image after restart.

What it can't do

The honest caveat: scaling a tiny source up to 8 megapixels with lanczos does not create detail. It interpolates - bigger pixels, same information. The KB's upscaling lore is blunt about this: if you want a small image at 8MP with actual recoverable detail, a generative upscaler (ESRGAN family, SeedVR2) is the tool, and even then there's a limit to what's in the source. Use this node when you're normalizing resolution, not when you're trying to invent detail. And remember fill will distort your aspect ratio, so double-check the output if you picked it in a hurry.

CategoryXJNodes/image

Inputs (5)

NameTypeDefaultDescription
imageIMAGE
megapixelsFLOAT1.000.01–16
round_toCOMBO85 options: 8, 16, 32, 64, 128
methodCOMBOfit3 options: fill, fit, crop
upscale_modeCOMBOlanczos5 options: lanczos, bicubic, bilinear, nearest-exact, area

Outputs (3)

NameTypeDescription
imageIMAGE
widthINT
heightINT