{"record":{"id":"cbc22c44805aa2cd","repo":"lllyasviel/ControlNet","slug":"image-must-have-h-x-w-x-3-h-x-w-x-1-or-h-x-w-dime","errorCode":null,"errorMessage":"Image must have H x W x 3, H x W x 1 or H x W dimensions.","messagePattern":"Image must have H x W x 3, H x W x 1 or H x W dimensions\\.","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"annotator/midas/utils.py","lineNumber":82,"sourceCode":"        scale (int, optional): Scale. Defaults to 1.\n    \"\"\"\n\n    with open(path, \"wb\") as file:\n        color = None\n\n        if image.dtype.name != \"float32\":\n            raise Exception(\"Image dtype must be float32.\")\n\n        image = np.flipud(image)\n\n        if len(image.shape) == 3 and image.shape[2] == 3:  # color image\n            color = True\n        elif (\n            len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1\n        ):  # greyscale\n            color = False\n        else:\n            raise Exception(\"Image must have H x W x 3, H x W x 1 or H x W dimensions.\")\n\n        file.write(\"PF\\n\" if color else \"Pf\\n\".encode())\n        file.write(\"%d %d\\n\".encode() % (image.shape[1], image.shape[0]))\n\n        endian = image.dtype.byteorder\n\n        if endian == \"<\" or endian == \"=\" and sys.byteorder == \"little\":\n            scale = -scale\n\n        file.write(\"%f\\n\".encode() % scale)\n\n        image.tofile(file)\n\n\ndef read_image(path):\n    \"\"\"Read image and output RGB image (0-1).\n\n    Args:","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/lllyasviel/ControlNet/blob/ed85cd1e25a5ed592f7d8178495b4483de0331bf/annotator/midas/utils.py#L64-L100","documentation":"write_pfm only supports color (HxWx3), greyscale single-channel (HxWx1), and plain 2D (HxW) images. Any other rank/channel count (e.g. HxWx4 RGBA, batched NxHxW, HxWx2) fails this check.","triggerScenarios":"Calling write_pfm with an array that has 4 channels, a leading batch dimension, or a squeezed axis producing shape (H, W, 2), etc.","commonSituations":"Passing a batched model output without indexing [0]; images with an alpha channel; intermediate feature maps with many channels.","solutions":["Remove the batch dimension: img = output[0] before writing","Convert RGBA to RGB or take a single channel: img[..., :3] or img[..., 0]","For multi-channel maps, select the channel of interest and write each slice separately"],"exampleFix":"# before\nwrite_pfm('d.pfm', model_output)  # shape (1, H, W)\n# after\nwrite_pfm('d.pfm', model_output[0])  # shape (H, W)","handlingStrategy":"validation","validationCode":"import numpy as np\nimg = np.asarray(image)\nassert img.ndim in (2, 3) and (img.ndim == 2 or img.shape[2] in (1, 3)), f'bad shape {img.shape}'","typeGuard":"def is_pfm_writable(img) -> bool:\n    import numpy as np\n    img = np.asarray(img)\n    return img.ndim == 2 or (img.ndim == 3 and img.shape[2] in (1, 3))","tryCatchPattern":"try:\n    write_pfm(path, img)\nexcept Exception as e:\n    if 'H x W' in str(e) and img.ndim == 3 and img.shape[0] == 1:\n        write_pfm(path, img[0])\n    else:\n        raise","preventionTips":["Squeeze batch dimensions at the pipeline boundary","Check image.shape before any save call"],"tags":["pfm","numpy","shape","image-io"],"backgroundTag":"invalid-image-shape","analyzedSha":"ed85cd1e25a5ed592f7d8178495b4483de0331bf","analyzedAt":"2026-08-27T12:58:54.167Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}