{"record":{"id":"4ce72507d6954c99","repo":"calesthio/OpenMontage","slug":"could-not-read-image-input-path","errorCode":null,"errorMessage":"Could not read image: {input_path}","messagePattern":"Could not read image: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"tools/enhancement/upscale.py","lineNumber":194,"sourceCode":"    # Image upscaling\n    # ------------------------------------------------------------------\n\n    def _upscale_image(\n        self,\n        input_path: Path,\n        output_path: Path,\n        scale: int,\n        model_name: str,\n        face_enhance: bool,\n        denoise_strength: float,\n    ) -> dict[str, Any]:\n        import cv2\n\n        upsampler = self._build_upsampler(scale, model_name, denoise_strength, face_enhance)\n\n        img = cv2.imread(str(input_path), cv2.IMREAD_UNCHANGED)\n        if img is None:\n            raise ValueError(f\"Could not read image: {input_path}\")\n\n        output, _ = upsampler.enhance(img, outscale=scale)\n        cv2.imwrite(str(output_path), output)\n\n        h, w = output.shape[:2]\n        return {\"output_width\": w, \"output_height\": h}\n\n    # ------------------------------------------------------------------\n    # Video upscaling\n    # ------------------------------------------------------------------\n\n    def _upscale_video(\n        self,\n        input_path: Path,\n        output_path: Path,\n        scale: int,\n        model_name: str,\n        face_enhance: bool,","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/calesthio/OpenMontage/blob/95e1c3d0ab93482159818560f6a8c8e866b9139f/tools/enhancement/upscale.py#L176-L212","documentation":"ValueError raised by the upscaler's _upscale_image when cv2.imread returns None. OpenCV silently returns None (instead of raising) for unreadable files — missing path, unsupported/corrupt format, or a format the opencv build lacks codecs for (e.g. 16-bit PNGs, some TIFF variants, WebP on minimal builds). The explicit check converts that silent None into a clear message naming the path.","triggerScenarios":"Calling the upscale tool with input_path that does not exist, has an unexpected extension (.heic, .avif), is a directory, is a zero-byte/corrupt file, or is encoded in a format the installed opencv-python cannot decode.","commonSituations":"Passing user uploads without validation, path resolution bugs (relative paths resolved against the wrong cwd), filenames with special characters mishandled by shell/quoting, or opencv-python-headless builds without codec support for exotic formats.","solutions":["Check `input_path.exists()` and that it is a non-empty regular file before calling upscale.","Re-encode the image to PNG/JPEG with an external tool (Pillow, ffmpeg, ImageMagick) if the source is HEIC/AVIF/exotic TIFF.","Verify OpenCV can decode it standalone: `python -c \"import cv2; print(cv2.imread('file.png') is not None)\"`.","Confirm the path is absolute or resolved against the intended base directory — imread does not error on relative-path mistakes, it just returns None."],"exampleFix":"# before\nresult = tool.run(input_path=Path(\"photo.heic\"), ...)  # cv2.imread -> None -> ValueError\n\n# after\nfrom PIL import Image\ninput_path = Path(\"photo.heic\")\nif input_path.suffix.lower() not in {\".png\", \".jpg\", \".jpeg\", \".webp\", \".bmp\", \".tiff\"}:\n    input_path = input_path.with_suffix(\".png\")\n    Image.open(\"photo.heic\").save(input_path)\nresult = tool.run(input_path=input_path, ...)","handlingStrategy":"validation","validationCode":"from pathlib import Path\nimport cv2\n\ndef readable_image(path: Path) -> bool:\n    return path.is_file() and path.stat().st_size > 0 and cv2.imread(str(path)) is not None","typeGuard":null,"tryCatchPattern":"try:\n    result = tool.run(input_path=p, ...)\nexcept ValueError as e:\n    if \"Could not read image\" in str(e):\n        raise ValueError(f\"Unsupported/corrupt image {p}; convert to PNG or JPEG first\") from e\n    raise","preventionTips":["Validate the file exists and decodes with cv2.imread before the upscale call.","Convert HEIC/AVIF/exotic inputs to PNG upstream with Pillow.","Pass absolute paths; never rely on cwd for input resolution."],"tags":["opencv","image","file-io","validation","upscale"],"backgroundTag":null,"analyzedSha":"95e1c3d0ab93482159818560f6a8c8e866b9139f","analyzedAt":"2026-08-15T06:31:20.014Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}