Nodes/NN-custom-nodes/Image Optimize
ComfyUI Node

Image Optimize

The node that fixes a bug you didn't know you had

By bandifiu·Created about a month ago·Updated about a month ago· 0
Image Optimize
  • images
  • images
contiguoustrue

Every so often a ComfyUI workflow throws a baffling error - usually something about a tensor not being contiguous - or runs slower than it should for no visible reason. "Image Optimize" is a one-trick utility that exists for exactly those moments: it makes sure your image tensor is stored in contiguous memory, which is the fix for both symptoms.

What it is

NNImageOptimize is a memory-format utility node in the NN/util category of the bandifiu/ComfyUI-NN-custom-nodes pack. The README is honest about how unglamorous it is: "Optimizes image tensors by ensuring they are stored in contiguous memory format. This can improve compatibility and performance for some operations."

How it works

PyTorch tensors have a memory layout, and not all operations produce a contiguous one. Transposes, permutations, and some slicing return views where the elements aren't stored in the order the tensor claims - the data is scattered across memory with strides. Most operations handle that transparently, but some (older CUDA kernels, certain resize and save paths, some custom nodes) assume contiguous memory and either error out or copy silently, and silent copies are how you get "why is this node so slow?"

The node's mechanism is exactly one decision: if the contiguous toggle is on (its default) and the input tensor isn't already contiguous, call .contiguous() - which materializes the tensor into a properly ordered memory block - and pass the result through. If the tensor is already contiguous, it passes through untouched with zero work. It's a no-op that costs nothing when there's nothing to fix.

The inputs

  • images - the image tensor (or batch) to normalize.
  • contiguous - boolean, default on. Off disables the conversion entirely, making it a pure passthrough.

Single images output, identical data, possibly different memory layout.

When to reach for it

Realistically you add this node in three situations:

  1. A node downstream is erroring with a contiguous-memory complaint or a mysterious shape/layout error - drop this in front of it and the error usually disappears.
  2. A specific operation is mysteriously slow - if you suspect a hidden copy is happening on every run, forcing contiguity once upstream is cheaper than the repeated internal copies.
  3. You're feeding an unfamiliar custom node and want to eliminate one class of compatibility failure before debugging anything else. It's a cheap "shotgun" step when isolating an issue.

Where it's not the answer: it doesn't speed up normal workflows that already produce contiguous tensors - most ComfyUI image operations are contiguous already, so in the happy path this node does literally nothing. Don't expect a magic performance boost by sprinkling it everywhere; that's not the job, and the pack's own README only claims it helps "for some operations."

Installing

Ships in bandifiu/ComfyUI-NN-custom-nodes:

cd ComfyUI/custom_nodes
git clone https://github.com/bandifiu/ComfyUI-NN-custom-nodes

Restart ComfyUI (or Manager → "NN-custom-nodes"). Deps: torch, numpy, pillow - no models. GPL-3.0, newer V3 backend API.

The honest advice: this is a debugging and compatibility tool, not a workflow staple. If you add it and see no change, remove it - a no-op node in the hot path is still a node, and graphs are easier to read without unnecessary wiring. Keep it in your back pocket for the day something tells you a tensor "isn't contiguous."

CategoryNN/util

Inputs (2)

NameTypeDefaultDescription
imagesIMAGE
contiguousBOOLEANtrue

Outputs (1)

NameTypeDescription
imagesIMAGE