lllyasviel/ControlNet · error · Exception
Malformed PFM header.
Error message
Malformed PFM header.
What it means
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.
Source
Thrown at annotator/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
- 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
Example fix
# before
# file contains 'PF\n1024 768' (no trailing space/newline)
# after
# rewrite header properly:
with open(p, 'wb') as f:
f.write(('PF\n%d %d\n' % (w, h)).encode()) Defensive patterns
Strategy: validation
Validate before calling
import re
with open(path, 'rb') as f:
assert f.readline().strip() in (b'PF', b'Pf')
assert re.match(rb'^(\d+)\s(\d+)\s$', f.readline()), 'malformed PFM dims' Try / catch
try:
img = read_pfm(path)
except Exception as e:
if 'Malformed PFM header' in str(e):
img = repair_or_reconvert(path)
else:
raise Prevention
- Prefer writing PFMs with write_pfm so headers are well-formed
- Validate dataset files once at load, skip/report bad files
When it happens
Trigger: 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.
Common situations: Corrupted or hand-edited PFM files; files written by tools that omit the trailing whitespace or use different separators; partially downloaded datasets.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Not a PFM file:
- Image dtype must be float32.
- Malformed PFM header.
- Image dtype must be float32.
- Image must have H x W x 3, H x W x 1 or H x W dimensions.
AI-assisted analysis of lllyasviel/ControlNet@ed85cd1e25 (2026-08-27).
Data as JSON: /api/errors/6b6ed3f5f33532b5.
Report an issue: GitHub.