Nodes/comfyui-ysnodes/ImageScaleToTotalPixels
ComfyUI Node

ImageScaleToTotalPixels

One node for scaling an image to target megapixels *and* encoding it to a latent

By MrYassinox·Created 2 months ago·Updated about a month ago· 1
ImageScaleToTotalPixels
  • pixels
  • mask
  • vae
  • image
  • latent_image
  • mask
  • latent_mask
upscale_methodlanczos
scale_by1.00
megapixels1.00
multiple8
enable_megapixelstrue

If you do img2img, inpainting, or hi-res fix in ComfyUI, you've met this pattern: scale the image down to a sensible size for your model, VAE-encode it, then feed the latent to a sampler. In a stock graph that's ImageScaleToTotalPixels followed by a VAEEncode - and if you're working with a mask, you get to add MaskToImage, another scale, another encode, and a couple of switches. This node from comfyui-ysnodes replaces that whole chain with one box. Its docstring says it replaces seven nodes, and that's not marketing.

It's a polished rewrite of comfyanonymous's famous core node - the one Comfy Org's own handbook suggests downscaling to 0.35 MP with before a SeedVR2 upscale. Where the core version hardcodes the rounding to 8 and only does the megapixel step, this one adds a scale_by multiplier and a multiple dial.

How it works

Two sequential, independently-toggleable steps:

  1. scale_by - multiplies the current size. 1.0 passes pixels through unchanged, byte-for-byte.
  2. megapixels - if enable_megapixels is on, rescales the step-one result so width × height ≈ megapixels × 1,048,576, aspect preserved.

Every result snaps to multiple (default 8 = VAE-safe). Then, if a vae is connected, the final image gets VAEEncoded into latent_image, and a connected mask follows the same path into latent_mask. The mask is scaled with the exact same parameters as the image, so the two always come out at matching dimensions - which is the whole battle with inpainting setups.

Outputs you get

  • image and mask - the resized pixels (mask is zeros if not connected).
  • latent_image and latent_mask - the VAE-encoded versions.

For a typical img2img redraw you take image → your sampler's latent path or a preview, and latent_image → the KSampler. Having both pixel and latent versions out of one node is handy for two-pass work where you want to see the resized result but sample from the latent.

The trap: leave the VAE unconnected and you get zeros

This is the one that bites. The vae input is optional, and the node's fallback when it's missing is to return zeros for latent_image and latent_mask - not an error. Wire latent_image into a KSampler without a VAE connected and you'll happily generate from a blank latent with no obvious clue why. If your img2img output looks like a black rectangle or pure noise, check that the VAE is actually plugged in here. The pack's sibling ImageScaleToNormPixels is the version that skips the VAE entirely when you only need pixels; grab that one when you don't need the latent side.

Installing it

Part of the comfyui-ysnodes pack, which has zero external dependencies - requirements.txt is empty, no model files to fetch.

cd ComfyUI/custom_nodes
git clone https://github.com/MrYassinox/comfyui-ysnodes.git

or search comfyui-ysnodes in ComfyUI Manager. Either way, restart ComfyUI and you'll find it under YSNodes/image.

When you'd actually reach for it

Inpainting and redraw workflows where you want the whole resolution + encode chain collapsed into one node, and hi-res-fix prep where you're normalizing a source to your model's native megapixel range before a second pass. Keep the default multiple of 8 - straying from it is how you get dimension-mismatch errors downstream when the latent doesn't divide cleanly.

CategoryYSNodes/image

Inputs (8)

NameTypeDefaultDescription
pixelsIMAGEInput image to scale.
upscale_methodCOMBOlanczosInterpolation method used when scaling.
scale_byFLOAT1.000.01–8Multiplies the image's current size. 1.0 = unchanged. 1.5 = 50% bigger. 0.5 = half size. Applied before the megapixels step below.
megapixelsFLOAT1.000.01–64Target resolution in megapixels, applied after scale_by. The image is scaled so width × height ≈ megapixels × 1,048,576, preserving the original aspect ratio. Example: 1.0 ≈ 1024×1024, 1.5 ≈ 1024×1536. Only takes effect when enable_megapixels=True.
multipleINT88–128Nearest multiple to round scaled width/height to (applies to both scale_by and megapixels). 8 = VAE-safe (recommended).
enable_megapixelsBOOLEANtrueWhen True → further scale the scale_by result to target megapixels. When False → use the scale_by result as-is, no megapixels constraint.
maskoptMASKOptional mask. Goes through the same scale_by + megapixels steps as the image. Returns zeros when not connected.
vaeoptVAEOptional VAE model for encoding outputs to latent space. latent_image and latent_mask return zeros when not connected.

Outputs (4)

NameTypeDescription
imageIMAGE
latent_imageLATENT
maskMASK
latent_maskLATENT