Nodes/Various ComfyUI Nodes by Type/Image Resize to Megapixels
ComfyUI Node

Image Resize to Megapixels

The 'Just Make It 1 Megapixel' Node

By jamesWalker55·Created 3 years ago·Updated 3 days ago· 218
Image Resize to Megapixels
  • image
  • IMAGE
  • WIDTH
  • HEIGHT
megapixels1.00
divisible_by32
interpolation_mode

Most resize nodes make you think in width and height. This one makes you think in pixels: give it any image and a target like 1, and it comes back at roughly one megapixel, whatever the aspect ratio. If you've ever stared at a non-square image and wished you could just say "make it a 1024×1024-sized image, I don't care about the exact numbers" - this is that, with the exact numbers still handed to you.

It's a pure resize utility from jamesWalker55's comfyui-various pack, and it fits the pack's personality: a grab-bag of small, boring, single-job nodes that mostly run in the background. Nobody makes threads about it - the pack shows up in install logs and workflow files, not in "what's the best node" posts - but that's exactly what a workhorse looks like.

What it's for (and what it isn't)

Per the upscaling taxonomy in the knowledge base, there are two very different jobs people call "upscaling": adding pixels (interpolation) and inventing detail (generative upscalers like SeedVR2 or SUPIR). This node is firmly in the first camp. It resizes with torchvision's F.resize and can only remap what's already there - it cannot add pores, fix softness, or repair faces. Use it when you need an image at a target size, not when you need it to look better. The detail job belongs to a real upscaler.

The specific niche: matching total pixel budget instead of a fixed dimension. Say you're running img2img and your model wants roughly 1MP of canvas. Resize your reference to 1MP here, then wire the WIDTH and HEIGHT outputs into an EmptyLatentImage - the node hands you the exact integers it computed, so your generation canvas lands at the same resolution as the resized input. That's the workflow this node exists for.

How it works

The math is one short block, and it's smarter than it looks:

  1. It computes your aspect ratio, then solves for the width and height that give exactly your target megapixels at that ratio (sqrt(megapixels × 1,000,000 / aspect) per side).
  2. It rounds both dimensions to the nearest divisible_by (default 32), never rounding to zero.
  3. Rather than risk losing a fraction of a pixel to rounding, it scales the image up to fully cover the target box, then center_crops down to the exact final size.

That cover-then-crop step is the bit to remember: it never stretches the image, and it never lands short of the target. The cost is a sliver of your edges trimmed away when the aspect ratio doesn't line up with the divisible grid. On a normal photo it's a few pixels; on something with a hard edge at the border it can matter.

Inputs that matter

You'll set two of the four inputs and leave the rest:

  • megapixels - the total you want, e.g. 1 for ~1MP. Float, so 0.5 for a half-megapixel downscale. This is the whole point of the node.
  • divisible_by - defaults to 32, which keeps dimensions friendly to most VAEs and latents. If you're feeding Flux, which wants multiples of 64, bump it to 64. Set it to 1 if you want the exact pixel count and can live with odd dimensions.
  • interpolation_mode - bicubic is the right default for photos. Pick nearest exact for pixel art or masks where you need hard edges and no blending.

Outputs are the resized IMAGE plus WIDTH and HEIGHT as integers - the part core ComfyUI's own ImageScaleToTotalPixels doesn't hand you as readily.

Install

No models to download and no requirements.txt - the node only needs torch, torchvision, and PIL, which are already part of ComfyUI. The author deliberately recommends not cloning the whole repo, since it populates your node list with a lot of stuff you may not want. His preferred path:

# grab just comfyui_image_ops.py from the repo and drop it in
ComfyUI/custom_nodes/comfyui_image_ops.py

then restart ComfyUI. Or clone everything:

cd ComfyUI/custom_nodes
git clone https://github.com/jamesWalker55/comfyui-various

It's also in the ComfyUI Manager registry under "Various ComfyUI Nodes by Type," though the single-file route is what the README actually pushes.

Gotchas

The README's node list is stale - it doesn't even mention this node (it lists the sibling resize nodes, but JWImageResizeToMegapixels shipped later in the same file). Don't panic if the docs don't show it; the class is right there in comfyui_image_ops.py.

And the one real trap: because of the divisible rounding, the output is approximately your target megapixels, not exactly. 1 megapixel at divisible_by: 32 can land a hair over. For almost everything that's fine; if you genuinely need a precise budget, that's what divisible_by: 1 is for.

CategoryjamesWalker55

Inputs (4)

NameTypeDefaultDescription
imageIMAGE
megapixelsFLOAT1.000.01–99999
divisible_byINT321–99999
interpolation_modeCOMBO4 options: bicubic, bilinear, nearest, nearest exact

Outputs (3)

NameTypeDescription
IMAGEIMAGE
WIDTHINT
HEIGHTINT