{"record":{"id":"6b7a24740f19da1e","repo":"roboflow/supervision","slug":"image-must-be-uint8-got-image-dtype-convert-wi","errorCode":null,"errorMessage":"image must be uint8, got {image.dtype}. Convert with image.astype(np.uint8) before calling show().","messagePattern":"image must be uint8, got (.+?)\\. Convert with image\\.astype\\(np\\.uint8\\) before calling show\\(\\)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/supervision/utils/image_window.py","lineNumber":108,"sourceCode":"\n    def show(self, image: npt.NDArray[np.uint8]) -> None:\n        \"\"\"Display a BGR, grayscale, or BGRA frame in the window.\n\n        The image is scaled to fit the current window dimensions. Aspect ratio is\n        preserved unless `keep_aspect_ratio=False`. Resizing the window rescales live.\n        Args:\n            image: uint8 numpy array. Accepted shapes:\n                - ``(H, W)`` — grayscale\n                - ``(H, W, 3)`` — BGR (OpenCV convention; channels are swapped\n                  to RGB before display)\n                - ``(H, W, 4)`` — BGRA (channels reordered to RGBA)\n\n        Raises:\n            TypeError: If `image.dtype` is not `uint8`.\n            ValueError: If `image` is not 2-D or 3-D with 3 or 4 channels.\n        \"\"\"\n        if image.dtype != np.uint8:\n            raise TypeError(\n                f\"image must be uint8, got {image.dtype}. \"\n                \"Convert with image.astype(np.uint8) before calling show().\"\n            )\n        self._pil_image = _bgr_to_pil(image)\n        self._ensure_window()\n        self._update_display()\n        self._root.update_idletasks()\n        self._root.update()\n\n    def wait_key(self, delay_ms: int = 0) -> str | None:\n        \"\"\"Wait for a keypress and return its name.\n\n        Args:\n            delay_ms: How long to wait in milliseconds. ``0`` blocks until a\n                key is pressed. Positive values poll for up to `delay_ms` ms\n                and return ``None`` if no key arrives in time.\n\n        Returns:","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/utils/image_window.py#L90-L126","documentation":"Raised by ImageWindow.show() in supervision.utils.image_window when the input array's dtype is not np.uint8. The Tk-based display path converts the array straight to a Pillow image, which requires 8-bit data; float arrays (common after model preprocessing) would render as garbage or fail inside Pillow, so the dtype is checked up front with an actionable message.","triggerScenarios":"Calling window.show(image) with a float32/float64 array (e.g. normalized 0-1 images, model outputs, cv2.resize on floats); uint16 medical/scientific imagery; boolean masks.","commonSituations":"Displaying frames that went through normalization (image / 255.0); annotator outputs assumed to stay uint8 but a preprocessing step cast them; stacking with np.mean producing float; feeding depth maps.","solutions":["Scale floats back to range then cast: (img * 255).clip(0, 255).astype(np.uint8).","For arbitrary floats use cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8).","Convert integer types directly: img.astype(np.uint8) only if values already span 0-255.","Keep a uint8 copy of the original frame for display alongside the float working copy."],"exampleFix":"// before\nwindow.show(frame / 255.0)  # float64 -> TypeError\n\n// after\nwindow.show((frame / 255.0 * 255).clip(0, 255).astype(np.uint8))","handlingStrategy":"type-guard","validationCode":"def ensure_uint8(img: np.ndarray) -> np.ndarray:\n    if img.dtype == np.uint8:\n        return img\n    if np.issubdtype(img.dtype, np.floating):\n        return (img * 255 if img.max() <= 1.0 else img).clip(0, 255).astype(np.uint8)\n    return img.astype(np.uint8)\n\nwindow.show(ensure_uint8(image))","typeGuard":"def is_uint8_image(image: np.ndarray) -> bool:\n    return image.dtype == np.uint8","tryCatchPattern":null,"preventionTips":["Keep an untouched uint8 copy of each frame for display.","Centralize float-to-uint8 conversion in one helper used before all visualization."],"tags":["gui","image-processing","dtype","numpy"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}