Nodes/ComfyUI_DCT/DCT 2D Transform
ComfyUI Node

DCT 2D Transform

Peek Inside Your Image's Frequencies Before You Erase Them

By bemoregt·Created 6 months ago·Updated 6 months ago· 1
DCT 2D Transform
  • image
  • dct_spectrum
  • spectrum_preview

DCT 2D Transform is the front door to the ComfyUI_DCT pack's party trick: editing an image in frequency space instead of pixel space. A normal image goes in, and out comes a map of how much energy sits in each frequency - the same math JPEG uses to compress, exposed as a node you can actually look at. On its own it does nothing you'd notice. Wired into the DCT Spectrum Mask Editor and Inverse DCT 2D nodes that ship alongside it, it's the reason the whole trick is possible.

Why you'd reach for it

Most image editing happens in pixel space: you sharpen, blur, or paint on the actual picture. Frequency-domain filtering is the same goal from the other side - instead of touching pixels, you change how much of each frequency the image contains. Remove a checkerboard or moiré pattern, clean sensor noise, kill JPEG ringing, sharpen without the halo that spatial sharpeners leave behind. All of those are "cut or boost certain frequencies" operations, and this pack turns them into paint strokes. DCT2D is the converter that gets you into that domain, and its preview output doubles as a decent diagnostic: it shows you where an image's energy actually lives before you start erasing.

How it works

The node runs a 2-D DCT-II on each colour channel using scipy.fft.dctn with norm='ortho' - the orthonormal normalization that makes the forward and inverse transforms exact inverses of each other. Low frequencies (flat structure, overall brightness, the DC component that dominates the whole map) pile up in the top-left corner; high frequencies (edges, noise, fine texture) spread toward the bottom-right.

The raw output, dct_spectrum, is real-valued coefficients, not an image - you feed it to the pack's other two nodes, not to a Preview Image node. The second output, spectrum_preview, is the visualization, and it's a thoughtful one. The DC coefficient dwarfs everything else by several orders of magnitude, so a naive min-max scale would render the preview almost entirely black. The node instead applies a log-scale and normalizes each channel at the 99th percentile, which is how you actually see the mid frequencies instead of one white pixel in the corner.

One honest caveat: this runs on the CPU. The code pulls the tensor off the GPU and transforms it in float64 via scipy. Totally fine for a single image, but don't feed it a big batch and expect speed.

The inputs and outputs that matter

  • image (IMAGE) - the only input. Any image tensor: a Load Image output, a VAE decode, whatever.
  • dct_spectrum (DCT_SPECTRUM) - the raw coefficients. Wire this into DCT Spectrum Mask Editor or Inverse DCT 2D.
  • spectrum_preview (IMAGE) - the log-scaled visualization, normalized to [0,1]. The one you actually look at.

Note that DCT_SPECTRUM is a custom type owned by this pack - nothing else in the ecosystem can produce or read it, so the three nodes form a closed chain. Don't expect to feed these coefficients into some other pack's filter.

Installation

Standard custom-node drill. In ComfyUI Manager, search "ComfyUI_DCT" and hit install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/bemoregt/ComfyUI_DCT
pip install scipy

Then restart ComfyUI. scipy (≥1.4.0) and Pillow are the only real dependencies; torch and numpy come with ComfyUI. scipy is frequently already in your environment because half the ecosystem drags it in, but the pack will clearly tell you if it isn't. The three nodes appear under the DCT category in the node search.

Common issues

  • "scipy is required for DCT nodes" - that's the pack's own error when scipy is missing. pip install scipy and restart.
  • The spectrum looks boring - you may be looking at the raw coefficient output instead of spectrum_preview. Only the preview is scaled for human eyes.
  • Slow on batches - it's CPU-bound float64 math. Run single images and keep moving.
CategoryDCT

Inputs (1)

NameTypeDefaultDescription
imageIMAGE

Outputs (2)

NameTypeDescription
dct_spectrumDCT_SPECTRUM
spectrum_previewIMAGE