diff --git a/pyproject.toml b/pyproject.toml index c83deb9f3..43b4d4e8c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "viam-sdk" -version = "0.4.4" +version = "0.4.5" description = "Viam Robotics Python SDK" authors = [ "Naveed " ] license = "Apache-2.0" diff --git a/src/viam/components/camera/client.py b/src/viam/components/camera/client.py index 76e08c73b..c3c76942b 100644 --- a/src/viam/components/camera/client.py +++ b/src/viam/components/camera/client.py @@ -26,8 +26,8 @@ def get_image_from_response(data: bytes, response_mime_type: str, request_mime_type: Optional[str] = None) -> Union[Image.Image, RawImage]: if request_mime_type is None: request_mime_type = response_mime_type - _, is_lazy = CameraMimeType.from_lazy(request_mime_type) - if is_lazy or not (CameraMimeType.is_supported(response_mime_type)): + mime_type, is_lazy = CameraMimeType.from_lazy(request_mime_type) + if is_lazy or mime_type._should_be_raw: image = RawImage(data=data, mime_type=response_mime_type) return image return Image.open(BytesIO(data), formats=LIBRARY_SUPPORTED_FORMATS) diff --git a/src/viam/media/video.py b/src/viam/media/video.py index 912ea3cd3..c1c104475 100644 --- a/src/viam/media/video.py +++ b/src/viam/media/video.py @@ -3,7 +3,7 @@ from io import BytesIO from typing import List, NamedTuple, Optional, Tuple, Union -from PIL import Image +from PIL import Image, UnidentifiedImageError from typing_extensions import Self from viam.errors import NotSupportedError @@ -94,6 +94,12 @@ def encode_image(self, image: Union[Image.Image, RawImage]) -> bytes: else: raise ValueError(f"Cannot encode image to {self}") + @property + def _should_be_raw(self) -> bool: + return self in [CameraMimeType.UNSUPPORTED, CameraMimeType.PCD, CameraMimeType.VIAM_RAW_DEPTH] or not CameraMimeType.is_supported( + self + ) + @classmethod def is_supported(cls, mime_type: str) -> bool: """Check if the provided mime_type is supported. @@ -185,7 +191,10 @@ def image(self) -> Optional[Image.Image]: self._image_decoded = True return self._image - self._image = Image.open(BytesIO(self.data), formats=LIBRARY_SUPPORTED_FORMATS) + try: + self._image = Image.open(BytesIO(self.data), formats=LIBRARY_SUPPORTED_FORMATS) + except UnidentifiedImageError: + self._image = None self._image_decoded = True return self._image