Z-Stack (Median/Mean)
Stack frames, keep the signal
- image_1
- stacked_image
If the noise is random and the image content isn't, averaging multiple captures cancels the noise out. It's how astrophotographers stack hundreds of photos to pull detail out of the dark, and it's exactly what Z-Stack (Median/Mean) does for images in your graph: feed it several versions of the same scene and get back one cleaner result. Each extra frame you connect is another vote, and the noise - being different every frame - loses the election.
Where does this come up in ComfyUI? Shooting the same render multiple times with different seeds and stacking them for denoise. Stacking microscopy or photo frames where the subject is static and the sensor isn't. Even removing a transient object - a car, a pedestrian - if you have multiple frames of an otherwise empty scene. It's a niche tool in the diffusion world, but when you need it, nothing else in the graph does it.
How it works
Connect your images and pick a mode:
- Median (default) - takes the per-pixel median across all frames. Robust to outliers: a bright speck in one frame doesn't drag the result, and a moving object that occupies different pixels per frame gets discarded entirely. This is the one for genuine noise reduction.
- Mean - a plain average. Faster, preserves all data, but one bad frame poisons the result. Use it when your frames are clean and you just want to smooth.
The inputs are dynamic: the node starts with image_1, and connecting it spawns image_2, then image_3, and so on, as far as you want to go. Wire up as many as you have; there's no fixed cap. The output is a single stacked_image.
One real constraint from the code: all images must have identical dimensions, and it only looks at the first frame of each batch - so if you feed it batched tensors, frame 0 of each batch is what gets stacked. For typical use (single images) that's invisible; for batch inputs it's a gotcha worth knowing.
The fine print
More frames is better up to a point, and the returns dim fast. Three to five frames gets you most of the win; twenty is barely better than ten, just slower. Median is more expensive than mean, so for a quick denoise of clean frames, mean is the pragmatic choice. And this only works when the content is aligned across frames - if the scene shifts, you're averaging blur, not noise. For static setups you're golden.
Install
Leputen Utils is lilquail's Windows-oriented texture toolkit. Install with ComfyUI Manager (search "ComfyUI-Leputen-Utils") or:
cd ComfyUI/custom_nodes
git clone https://github.com/lilquail/ComfyUI-Leputen-Utils
Restart ComfyUI. This node is a torch median/mean over a stack - no model files, no optional dependencies.
Where people get burned
The #1 mistake is expecting it to fix misaligned frames - it can't, and averaging misaligned content gives you ghosting. Second is feeding it a single image and wondering why nothing happened: one image passes through unchanged (the code explicitly returns it as-is). You need at least two for there to be a stack, and the mode's whole point only shows up with three or more. Third is the shape mismatch error - if your frames are even a pixel different, it throws rather than guessing, so normalize sizes (say, with the pack's Resize Power of 2) before stacking.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| mode | COMBO | Median | Median: More robust to outliers, removes transient objects. Mean: Simple averaging, faster computation. |
| image_1opt | IMAGE | First image to stack. Connect more images below. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| stacked_image | IMAGE | — |