Nodes/opencv-comfyui/OpenCV log_0
ComfyUI Node

OpenCV log_0

The natural-log transform nobody puts in their workflow (until they need it)

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV log_0
  • src
  • dst
  • nparray

This one is about as minimal as a node gets. OpenCV log_0 wraps cv2.log, which applies the natural logarithm to every pixel value, element-wise. Feed it an image, get the same-size image back where each value is ln(x). That's the whole node - two inputs, one output, no knobs to turn.

Before you dismiss it as math-flavored filler: the log transform is the classic way to make very bright images look sensible. It's the standard move in HDR display and the reason astronomers log-stretch deep-space photos. A log curve compresses the top of the range - the differences between 200 and 255 (which the eye can't separate anyway) get pulled apart down low, where 1 to 50 matters. If you're feeding an overexposed render or a raw-ish float buffer into a pipeline and everything looks blown out, a log pass flattens the range into something workable.

It's also a genuinely useful helper inside math chains - Fourier magnitudes, feature maps, anything where you want to compress dynamic range before a later operation instead of normalizing blindly.

How it works

Element-wise: dst(x, y) = ln(src(x, y)). Two real gotchas hide in that formula. First, ln(0) is undefined, so black pixels become -inf (or nan). OpenCV clips/handles this in practice, but a fully black image is a degenerate input - don't feed one. Second, the output is a float32 array, not an 8-bit image. cv2.log doesn't auto-convert back to uint8, so you'll need to normalize (scale + shift) before the result is viewable, and it will not look like a normal photo until you do.

Inputs:

  • src - NPARRAY in. (Image2Nparray / Nparrays2Image to cross the boundary.)
  • dst - optional out-parameter; skip it. Output is the single nparray.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python

Restart, or install via ComfyUI Manager (search "opencv"). Requires opencv-contrib-python, numpy, torch. No models.

Common issues

  • Output looks like a black box or garbage - it's a float array with values far outside 0–255. Normalize it (a stretch-to-range node, or normalize_0 from this same pack) before previewing.
  • 'NoneType' object has no attribute 'shape' - the README's signature warning: not every nparray coming out of this pack is an image you can display. Here, normalize first.
  • Batch error - batch_size==1 only; ImageFromBatch (length=1).

Fair take: you'll probably go a long time without touching log_0. But when you're fighting a blown-out float buffer or want to pre-compress a spectrum before doing something clever, it's the exact tool, already in your graph. And the _1 variant next to it is the same node - the pack numbers OpenCV's two overloads instead of merging them.

Categoryimage/OpenCV

Inputs (2)

NameTypeDefaultDescription
srcNPARRAY
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY