小珠光图片缩放高速版
Resize to an aspect ratio, not just a pixel count — batched, fast, masks in lockstep
- image
- mask
- 图像
- 遮罩
- 宽度
- 高度
ComfyUI's built-in ImageScale is fine when you already know the exact pixels you want. But a lot of real pipelines are keyed to shape, not pixel count: "make my reference image 16:9," "pad this input to 1:1 because the model expects a square," "fit every frame of this batch to the same ratio." That's the job 小珠光图片缩放高速版 - XiaozhuguangImageScaleByAspectRatioV2 - does, and the "高速版" part means it does it as one batched torch operation instead of converting each image to PIL and back.
It's the pack's second-generation aspect-ratio scaler: the earlier version ground through PIL image by image, this one pushes the whole batch through torch.nn.functional.interpolate in a single call. For what it's worth, this is the same deterministic geometry work you'd do in Photoshop or ImageMagick - no diffusion pass, no randomness, millisecond-fast. It's infrastructure, and it quietly unblocks a bunch of workflows that assume inputs arrive at a fixed ratio.
How it works
You pick a target ratio, then a rule for how big the result should be, then a fit mode, and the node computes a (width, height) and resizes the whole batch in one shot.
The ratio itself comes from the aspect_ratio dropdown, which is worth reading carefully because the 宽度/高度 widgets mean different things in different modes:
- 尺寸输入 - skip the ratio logic entirely; 宽度 and 高度 are the target pixels.
- 原始比例 - keep the source's own aspect ratio.
- 自定义比例 - treat 宽度/高度 as the ratio you want (e.g. 16 and 9).
- The six presets (1:1, 3:2, 4:3, 16:9, 2:3, 3:4, 9:16) - fixed ratios, no math on your part.
Then scale_to_side + scale_to_length decide the size: longest / shortest / width / height say which edge should equal scale_to_length (default 1024), and total_pixel(kilo pixel) treats it as a kilopixel budget. round_to_multiple rounds the result up to a multiple of 8/16/32/…/512 - handy for hitting a model's preferred latent dimensions.
fit picks the geometry: letterbox scales to fit inside the box and pads the leftover with background_color (default #000000), crop center-crops to fill the box, fill stretches to the target and distorts. For a mask input it does the same pass so the mask and image stay perfectly aligned.
For the actual resize, nearest, bilinear, and bicubic go through the batched torch path; lanczos isn't a torch interpolation mode, so it silently falls back to per-image PIL - same result, slower. Use lanczos only when quality on a small number of images justifies it.
The inputs and outputs that matter
- aspect_ratio, fit, scale_to_side, scale_to_length - the four you'll actually touch most runs.
- image / mask (both optional) - at least one is required; if both are present they must be the same size or the node raises.
- round_to_multiple, background_color - set-and-forget once you know your model's constraints.
Outputs: 图像 (IMAGE), 遮罩 (MASK), plus 宽度 and 高度 as INTs. Those last two are the underrated part - wire them into a latent or KSampler so everything downstream agrees on the actual dimensions instead of recomputing them.
Where people get burned
The classic footgun: aspect_ratio set to 尺寸输入 with 宽度/高度 left at their defaults of 1. You get a 1×1 image. There's no validation protecting you - in that mode the widget values are the target. Set them before you queue.
Second: if you need speed on a big batch, don't leave method on lanczos - it's the PIL fallback path and per-image. bicubic gets you torch-batched results with quality that's close enough for a resize.
Installing it
It's part of the ComfyUI-xiaozhuguang pack (小珠光), a Chinese-community enhancement pack - install the pack, not the node:
cd ComfyUI/custom_nodes
git clone https://github.com/xiaozhuguang/ComfyUI-xiaozhuguang.git
then restart ComfyUI, or search ComfyUI-xiaozhuguang in ComfyUI Manager's custom-node installer. The pack's requirements pull in heavy dependencies (transformers, llama-cpp-python, librosa), but those serve the Qwen and TTS nodes - this scaler only needs torch, numpy, and Pillow, which you already have. No models to download, no external API.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| aspect_ratio | COMBO | 10 options: 尺寸输入, 原始比例, 自定义比例, 1:1, 3:2, 4:3, +4 | |
| 宽度 | INT | 11–100000000 | — |
| 高度 | INT | 11–100000000 | — |
| fit | COMBO | 3 options: letterbox, crop, fill | |
| method | COMBO | 4 options: lanczos, bicubic, bilinear, nearest | |
| round_to_multiple | COMBO | 8 options: 8, 16, 32, 64, 128, 256, +2 | |
| scale_to_side | COMBO | 6 options: None, longest, shortest, width, height, total_pixel(kilo pixel) | |
| scale_to_length | INT | 10244–100000000 | — |
| background_color | STRING | #000000 | — |
| imageopt | IMAGE | — | |
| maskopt | MASK | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| 图像 | IMAGE | — |
| 遮罩 | MASK | — |
| 宽度 | INT | — |
| 高度 | INT | — |