Nodes/ComfyUI-QG-tools/图像或 Latent 尺寸
ComfyUI Node

图像或 Latent 尺寸

Reading size off an image, mask or latent

By live1617·Created 9 days ago·Updated a day ago· 1
图像或 Latent 尺寸
  • input
  • WIDTH
  • HEIGHT

Sooner or later you want width and height as wires. Not as numbers you typed into a widget three nodes ago - as the actual dimensions of the thing flowing through the graph, so that the upscale factor, the blur radius, or the paste coordinates adapt when the image does. Core ComfyUI doesn't give you that in one node. 图像或 Latent 尺寸 (QG_ImageOrLatentSize) does, and it does it in the least fussy way possible: one wildcard input, two INTs out.

How it works

The input is declared as *, ComfyUI's wildcard, so it accepts an IMAGE, a MASK or a LATENT without you telling it which. The node inspects what arrives:

  • A latent is a dict with a samples tensor shaped (batch, channels, height, width). Those dimensions are the latent-space ones, so the node multiplies by 8 on the way out - a latent that is internally 128×96 reports 1024×768.
  • A tensor is read by shape. Four dimensions where the second axis is a single channel is treated as a mask and read as (B, 1, H, W); anything else four-dimensional is treated as an image in (B, H, W, C).

That last branch is a heuristic rather than a declaration, and it's worth knowing about. A batch of genuinely single-channel images would be misread as a mask - in practice that doesn't happen in ComfyUI image pipelines, where images are RGB, but it's the reason the check looks strange when you read the source.

Nothing connected, or something it doesn't recognise, returns (0, 0) rather than raising. That's convenient in a half-built graph and it means a downstream divide-by-dimension will still blow up somewhere else - the zero gets passed along.

Inputs and outputs

  • input (optional, wildcard) - the image, mask or latent to measure.
  • WIDTH (INT) - width in pixels, latent already scaled back up.
  • HEIGHT (INT) - height in pixels.

Where it earns its keep: driving a size-conditional branch (feed the numbers into a comparison, then into an if node), feeding a crop or paste node that needs coordinates, or sanity-checking that a resize actually did what you asked - wire WIDTH into a 展示任意 preview and you can read it on the canvas without rerouting the whole graph.

The 8× thing, explained properly

This trips up everyone once. Diffusion models don't work at pixel resolution - a VAE compresses the image 8× in each direction before the model ever sees it. An "empty latent" for a 1024×1024 output is a tensor of 128×128. Every latent producer in ComfyUI speaks in those compressed units internally, and every node that reports sizes has to decide whether to show you pixels or latent cells.

This node shows you pixels. The KB's plumbing notes make the point that most width/height confusion in ComfyUI comes from exactly this conversion being implicit; here it's explicit and done for you. If you ever compare these outputs against a node that hands you raw latent dimensions, expect them to differ by a factor of eight.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/live1617/ComfyUI-QG-tools

Full ComfyUI restart afterwards; the console prints [QG-tools] v0.9.2 加载完成 | 节点数 24. Manager users can search ComfyUI-QG-Tools where the Registry listing exists. Nothing to pip install - the pack's requirements file is comments only, so Manager's pip step does nothing; the node itself needs only torch.

This one came from rgthree-comfy (MIT), renamed to the QG prefix. Search size, width, height or 尺寸.

Where people get burned

Wiring the output into a widget instead of a socket. ComfyUI will only accept an INT link where an input has been converted to accept one. If the target node's width is still a plain widget, right-click it and convert to input first - that mechanic is the whole reason a size reader like this is useful.

Expecting 0 to be an error. It isn't. Unconnected returns (0, 0) silently, so a graph that misbehaves at 0×0 dimensions usually has a missing wire right here.

Assuming it corrects for non-8-divisible sizes. It reports what's there. If your pipeline produced 1000×1000 pixels, the latent is 125×125 and back-converting gives 1000 - no rounding nudge happens. Sizes that aren't divisible by 8 get rounded by whatever produced the latent, and this node will faithfully report the rounded result, not your original intent.

CategoryQG-tools/Image

Inputs (1)

NameTypeDefaultDescription
inputopt*接入图像(IMAGE)、遮罩(MASK)或 Latent,节点输出其宽度和高度。

Outputs (2)

NameTypeDescription
WIDTHINT接入内容的宽度(Latent 已自动 ×8)。
HEIGHTINT接入内容的高度(Latent 已自动 ×8)。