{"record":{"id":"6b6ed3f5f33532b5","repo":"lllyasviel/ControlNet","slug":"malformed-pfm-header","errorCode":null,"errorMessage":"Malformed PFM header.","messagePattern":"Malformed PFM header\\.","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"annotator/midas/utils.py","lineNumber":38,"sourceCode":"        color = None\n        width = None\n        height = None\n        scale = None\n        endian = None\n\n        header = file.readline().rstrip()\n        if header.decode(\"ascii\") == \"PF\":\n            color = True\n        elif header.decode(\"ascii\") == \"Pf\":\n            color = False\n        else:\n            raise Exception(\"Not a PFM file: \" + path)\n\n        dim_match = re.match(r\"^(\\d+)\\s(\\d+)\\s$\", file.readline().decode(\"ascii\"))\n        if dim_match:\n            width, height = list(map(int, dim_match.groups()))\n        else:\n            raise Exception(\"Malformed PFM header.\")\n\n        scale = float(file.readline().decode(\"ascii\").rstrip())\n        if scale < 0:\n            # little-endian\n            endian = \"<\"\n            scale = -scale\n        else:\n            # big-endian\n            endian = \">\"\n\n        data = np.fromfile(file, endian + \"f\")\n        shape = (height, width, 3) if color else (height, width)\n\n        data = np.reshape(data, shape)\n        data = np.flipud(data)\n\n        return data, scale\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/lllyasviel/ControlNet/blob/ed85cd1e25a5ed592f7d8178495b4483de0331bf/annotator/midas/utils.py#L20-L56","documentation":"After the PFM magic header, read_pfm expects the second line to be '<width> <height> ' matching regex ^(\\d+)\\s(\\d+)\\s$. If the dimension line is malformed the file cannot be parsed and this exception is raised.","triggerScenarios":"Calling read_pfm on a PFM whose dimension line is missing, has non-numeric tokens, extra fields, or a truncated file where the second line is empty.","commonSituations":"Corrupted or hand-edited PFM files; files written by tools that omit the trailing whitespace or use different separators; partially downloaded datasets.","solutions":["Inspect the first two lines of the file (e.g. head -c 64 file.pfm) and confirm the format 'W H '","Re-export or re-download the PFM from its source","If you control the writer, write the header with '%d %d\\n' as write_pfm does"],"exampleFix":"# before\n# file contains 'PF\\n1024 768' (no trailing space/newline) \n# after\n# rewrite header properly:\nwith open(p, 'wb') as f:\n    f.write(('PF\\n%d %d\\n' % (w, h)).encode())","handlingStrategy":"validation","validationCode":"import re\nwith open(path, 'rb') as f:\n    assert f.readline().strip() in (b'PF', b'Pf')\n    assert re.match(rb'^(\\d+)\\s(\\d+)\\s$', f.readline()), 'malformed PFM dims'","typeGuard":null,"tryCatchPattern":"try:\n    img = read_pfm(path)\nexcept Exception as e:\n    if 'Malformed PFM header' in str(e):\n        img = repair_or_reconvert(path)\n    else:\n        raise","preventionTips":["Prefer writing PFMs with write_pfm so headers are well-formed","Validate dataset files once at load, skip/report bad files"],"tags":["pfm","midas","corrupt-file","parsing"],"backgroundTag":"corrupt-file-header","analyzedSha":"ed85cd1e25a5ed592f7d8178495b4483de0331bf","analyzedAt":"2026-08-27T12:58:54.167Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}