Nodes/opencv-comfyui/OpenCV imcount_0
ComfyUI Node

OpenCV imcount_0

How many images are hiding in that file? A tiny but real utility

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV imcount_0
    • int
    filename
    flags

    imcount_0 answers one boring question: how many images are stored inside a single file? A GIF can hold a hundred frames. A multi-page TIFF can hold a scan of a 50-page document. An animated WebP holds a video's worth of frames. cv2.imcount counts them, and this node is the wrapper.

    That's the whole thing. It's not glamorous, but it's genuinely useful when you're doing batch file work inside a ComfyUI graph: before you decide to decode a multi-frame file frame by frame, you want to know how many frames there are, and this node hands you an INT you can feed into a loop or a condition. If you've ever scripted image processing in plain Python you've probably written the same check by hand; this just puts it on the canvas.

    The honest positioning, though: this pack wraps every top-level OpenCV function with zero polish, and most of them - this one included - are for people doing real computer-vision plumbing in ComfyUI, not for pretty final renders. If you're just loading a normal PNG, the built-in Load Image is the right node. imcount_0 earns its keep in the niche case where your source is a multi-frame file and you need to know its depth before processing.

    How it works. It's a direct call to cv2.imcount(filename, flags), so it reads the file header and counts the embedded images without decoding all of them. Two inputs, both required:

    • filename (STRING) - a filesystem path. Because it's a raw path (not ComfyUI's Load Image dialog), it can point anywhere the server can read - an absolute path, a path outside ComfyUI/input, whatever. Relative paths resolve against the ComfyUI working directory, so absolute paths are safer.
    • flags (INT) - the same IMREAD_* flags that imread uses. The default color read is 1 (IMREAD_COLOR); for counting purposes the default 0 (IMREAD_GRAYSCALE) is fine too. Honestly, 1 is the safe pick.

    Output. One INT named int - the image count. Wire it into any integer input: a primitive, a comparison, a loop bound.

    Installing. Via ComfyUI Manager (search "opencv-comfyui"), or:

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

    Restart ComfyUI; it appears under image/OpenCV.

    Where people get burned. Two things, both predictable. First, imcount on a single-frame PNG returns 1, so if you were hoping it'd tell you something about a normal image, it won't - that's not what it's for. Second, if the file can't be found or isn't readable by the ComfyUI server process, OpenCV returns -1 and you'll get a -1 through the node; the error messages here are terse. Check that the path actually exists on the machine running ComfyUI, not just on your browser's machine - that's the classic headless-server mistake. A batch-of-one note doesn't apply here since the only inputs are a string and an int; this node is one of the few in the pack that touches no image tensors at all.

    If you find yourself wanting this, you'll probably also want imread_0 (read a file into an nparray) and imdecode_0 (decode from an in-memory buffer) in the same pack - they're the read-half of the same file-IO story.

    Categoryimage/OpenCV

    Inputs (2)

    NameTypeDefaultDescription
    filenameSTRING
    flagsINT

    Outputs (1)

    NameTypeDescription
    intINT