Nodes/ComfyUI-YogurtNodes/Image Scale To Total Pixels Advanced (Yogurt Nodes)
ComfyUI Node

Image Scale To Total Pixels Advanced (Yogurt Nodes)

Resize to a pixel budget, not a resolution — with a mask proving it

By yogurt7771·Created 2 years ago·Updated 9 days ago· 1
Image Scale To Total Pixels Advanced (Yogurt Nodes)
  • image
  • image
  • mask
  • width
  • height
  • channels
  • longest_side
  • shortest_side
  • aspect_ratio
  • pixels
upscale_methodlanczos
megapixels1.0
divide_by1
pad_value#000000

Most resize nodes ask for a width and height. This one asks for a budget: "make this image about 1 megapixel," and it works out the width and height itself. Image Scale To Total Pixels Advanced (Yogurt Nodes) resizes any image to a target total pixel count, keeps the dimensions divisible by whatever you need for a latent model, and - the clever part - pads the short side and hands you a mask telling you exactly where the real image ended up.

Why is this useful? Because a lot of models and upscalers care about total pixels more than specific dimensions, and because latent models care about divisibility. Want to feed a sampler images that total ~1MP with dimensions divisible by 16? This does both in one node, and the mask output (white = valid pixels, black = padding) means you can composite the result back or crop off the pad downstream. The "Advanced" in the name is the padding + mask combo; plain ImageScaleToTotalPixels in the core just rescales.

How it works

It rescales the image so width × height lands at your megapixels target (1.0 = 1,048,576 pixels, matching the standard convention), using the chosen upscale_method (lanczos default, plus nearest-exact, bilinear, area, bicubic). Then, if divide_by is greater than 1, it rounds the dimensions up to a multiple of that number - and here's the thoughtful bit: it computes both the width-based and height-based options, picks whichever needs less padding, and pads only the short side with your pad_value (a #RRGGBB hex color, e.g. #000000). The output mask is 1.0 where the image is and 0.0 on the pad, so downstream nodes know the true content region.

The inputs that matter

  • image - the batch to rescale.
  • megapixels - the target budget. 1.0 is the common default; 0.5 for lighter loads, 2.0+ for detail-heavy work.
  • divide_by - divisibility constraint; 8/16/32/64 depending on your model. Set 1 to skip the constraint entirely.
  • pad_value - the pad color. #000000 is black; pick something close to the image edge color to make the padding blend in.
  • upscale_method - interpolation; lanczos is the safe default.

Nine outputs: the image and mask, then size info - width, height, channels, longest_side, shortest_side, aspect_ratio, and pixels - so you can wire the actual result dimensions elsewhere without re-measuring.

Installing it

It's part of ComfyUI-YogurtNodes. Install via ComfyUI Manager (search "ComfyUI-YogurtNodes") or:

cd ComfyUI/custom_nodes
git clone https://github.com/yogurt7771/ComfyUI-YogurtNodes.git
cd ComfyUI-YogurtNodes
pip install -r requirements.txt

Restart ComfyUI; it's under "Yogurt Nodes". No downloads.

Where people get burned

The pad_value format is strict: it must be #RRGGBB or #RGB - anything else raises a parse error, so don't paste in a color name. And remember the divisibility padding changes the image's total pixels: you asked for 1MP, but divide_by 16 can push the output slightly over as it rounds up - that's expected, not a bug. If you're chaining this into a compositor, wire the mask output through so the pad region doesn't pollute the composite. One more: megapixels of 0 is technically accepted by the field but produces a degenerate result, so keep it sane. Other than that, this is one of the more genuinely useful utility nodes in the pack.

CategoryYogurtNodes/Image

Inputs (5)

NameTypeDefaultDescription
imageIMAGEThe images to batch.
upscale_methodCOMBOlanczos5 options: nearest-exact, bilinear, area, bicubic, lanczos
megapixelsFLOAT1.0
divide_byINT11–2147483647
pad_valueSTRING#000000

Outputs (9)

NameTypeDescription
imageIMAGE
maskMASK
widthINT
heightINT
channelsINT
longest_sideINT
shortest_sideINT
aspect_ratioFLOAT
pixelsINT