Nodes/opencv-comfyui/OpenCV sort_0
ComfyUI Node

OpenCV sort_0

Pixel-sort a photo inside ComfyUI — the glitch art node

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

Here's the rare auto-generated OpenCV node that's actually fun: sort_0 wraps cv2.sort, which sorts the rows or columns of a matrix. Feed it an image converted to an nparray, tell it to sort every column, convert back, and you've got the classic "sorted pixels" glitch effect - those smeared, rainbow-streak distortions you see in vaporwave art and music videos. It's a real post-processing look, it's deterministic, it costs milliseconds, and it needs no model at all. That fits the post-processing doc's rule perfectly: reach for the cheap primitive, not a diffusion pass.

Beyond glitch art, cv2.sort is a general matrix utility: sort a data array you're using to drive part of a workflow, order a set of values before feeding them somewhere that expects monotonic input. But let's be honest - in ComfyUI you're here for the pixel sorting.

The inputs

  • src (NPARRAY) - the array to sort. From an image: wire Image2Nparraysort_0. Reminder that Image2Nparray gives you a BGR uint8 array and only works on batch size 1.
  • flags (INT) - this is the whole trick. It's a bitmask of two choices:
    • Sort direction: 0 = ascending, 16 = descending (CV_SORT_DESCENDING).
    • Sort axis: 0 = every row independently, 1 = every column independently (CV_SORT_EVERY_COLUMN).

So the values that matter are 0 (rows, ascending), 1 (columns, ascending), 16 (rows, descending), 17 (columns, descending). For the streak effect you want 1 or 17 - sorting down each column turns vertical pixel runs into smooth gradients.

  • dst (NPARRAY, optional) - an out-parameter; leave it unwired and read the output. The pack's README flatly recommends avoiding out-parameters.

Output: one nparray - the sorted matrix, same shape as src.

Wiring the effect

Image2Nparraysort_0 (flags 1) → Nparrays2Image → any preview. That's it. Note the pack's color quirk: Image2Nparray reverses channels to BGR and Nparrays2Image reverses back, so the round trip keeps colors sane. If you want to sort only a mask region (the proper "pixel sorting" look), grab the nparray, threshold a copy, and use it as a mask before sorting - plain sort_0 sorts the whole frame.

Install

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

or ComfyUI Manager → "opencv-comfyui". Restart. Deps: opencv-contrib-python, numpy, torch - nothing to download.

Troubleshooting

  • Only images with batch_size==1 are supported! - Image2Nparray wants a single image; use ImageFromBatch (length=1) upstream.
  • No visible change - you're sorting rows (flags 0) on an image whose rows are already near-constant; try 1 or 17.
  • error: (-215:Assertion failed) - an upstream function demanded a grayscale input; convert with cvtColor code 6.
  • Cannot import name 'guidedFilter' at startup - conflicting OpenCV packages; README links the fix.

Cheap, deterministic, and it makes genuinely striking images. This is the sleeper node of the pack.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY
flagsINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY