lllyasviel/ControlNet · error · Exception

Malformed PFM header.

Error message

Malformed PFM header.

What it means

Raised by read_pfm when the second line of a PFM (Portable Float Map) file does not match the expected 'width height' dimensions pattern (two integers separated by whitespace with trailing newline). The PFM format requires a header of magic number, dimensions line, and scale/endianness line; this error means the dimensions line is corrupt, missing, or the file is truncated.

Source

Thrown at ldm/modules/midas/utils.py:38

        color = None
        width = None
        height = None
        scale = None
        endian = None

        header = file.readline().rstrip()
        if header.decode("ascii") == "PF":
            color = True
        elif header.decode("ascii") == "Pf":
            color = False
        else:
            raise Exception("Not a PFM file: " + path)

        dim_match = re.match(r"^(\d+)\s(\d+)\s$", file.readline().decode("ascii"))
        if dim_match:
            width, height = list(map(int, dim_match.groups()))
        else:
            raise Exception("Malformed PFM header.")

        scale = float(file.readline().decode("ascii").rstrip())
        if scale < 0:
            # little-endian
            endian = "<"
            scale = -scale
        else:
            # big-endian
            endian = ">"

        data = np.fromfile(file, endian + "f")
        shape = (height, width, 3) if color else (height, width)

        data = np.reshape(data, shape)
        data = np.flipud(data)

        return data, scale

View on GitHub (pinned to ed85cd1e25)

Solutions

  1. Inspect the first 2-3 lines of the file (e.g. with open(path,'rb').readline() in a REPL) to see the actual bytes of the dimensions line
  2. If line endings are CRLF, convert with dos2unix or rewrite the header to use '\n' and a single trailing space as the regex requires
  3. If the file is truncated or corrupt, regenerate it from the source (re-run MiDaS write_depth / download again)
  4. If producing PFM yourself, mimic midas.utils.write_pfm: 'PF\n' or 'Pf\n', then '%d %d\n' % (width, height), then the scale line

Example fix

# before: file with header 'PF\r\n640 480\r\n-1.0\r\n' fails read_pfm
# after: normalize line endings before parsing
with open(path, 'rb') as f:
    raw = f.read()
raw = raw.replace(b'\r\n', b'\n')
import io
data = read_pfm(io.BytesIO(raw))  # or rewrite to a fixed file
Defensive patterns

Strategy: validation

Validate before calling

import re
def pfm_header_ok(path):
    with open(path, 'rb') as f:
        magic = f.readline()
        if magic.strip() not in (b'PF', b'Pf'):
            return False
        return re.match(rb'^(\d+)\s(\d+)\s$', f.readline()) is not None

Try / catch

try:
    depth = read_pfm(path)
except Exception as e:
    if 'PFM' in str(e):
        raise IOError(f'Bad PFM file {path}: {e}')
    raise

Prevention

When it happens

Trigger: Calling midas.utils.read_pfm(path) on a file that passed the magic-number check ('PF'/'Pf') but whose second line fails the regex r'^(\d+)\s(\d+)\s$' — e.g. dimensions on one line with the scale, CRLF line endings, extra spaces after the line, or a file edited/corrupted after the header.

Common situations: Using MiDaS depth outputs that were written by a non-standard PFM writer, files transferred with text-mode FTP that mangled line endings, partially downloaded/truncated .pfm files, or hand-crafted test files where header lines were joined.

Understand the failure class

Related errors


AI-assisted analysis of lllyasviel/ControlNet@ed85cd1e25 (2026-08-27). Data as JSON: /api/errors/94e7992c43aa193c. Report an issue: GitHub.