Skip to content

Commit 715af06

Browse files
committed
[hotfix] Make it such that depth images always return RawImage (#361)
1 parent 434a9d9 commit 715af06

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/viam/components/camera/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
def get_image_from_response(data: bytes, response_mime_type: str, request_mime_type: Optional[str] = None) -> Union[Image.Image, RawImage]:
2727
if request_mime_type is None:
2828
request_mime_type = response_mime_type
29-
_, is_lazy = CameraMimeType.from_lazy(request_mime_type)
30-
if is_lazy or not (CameraMimeType.is_supported(response_mime_type)):
29+
mime_type, is_lazy = CameraMimeType.from_lazy(request_mime_type)
30+
if is_lazy or mime_type._should_be_raw:
3131
image = RawImage(data=data, mime_type=response_mime_type)
3232
return image
3333
return Image.open(BytesIO(data), formats=LIBRARY_SUPPORTED_FORMATS)

src/viam/media/video.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import BytesIO
44
from typing import List, NamedTuple, Optional, Tuple, Union
55

6-
from PIL import Image
6+
from PIL import Image, UnidentifiedImageError
77
from typing_extensions import Self
88

99
from viam.errors import NotSupportedError
@@ -94,6 +94,12 @@ def encode_image(self, image: Union[Image.Image, RawImage]) -> bytes:
9494
else:
9595
raise ValueError(f"Cannot encode image to {self}")
9696

97+
@property
98+
def _should_be_raw(self) -> bool:
99+
return self in [CameraMimeType.UNSUPPORTED, CameraMimeType.PCD, CameraMimeType.VIAM_RAW_DEPTH] or not CameraMimeType.is_supported(
100+
self
101+
)
102+
97103
@classmethod
98104
def is_supported(cls, mime_type: str) -> bool:
99105
"""Check if the provided mime_type is supported.
@@ -185,7 +191,10 @@ def image(self) -> Optional[Image.Image]:
185191
self._image_decoded = True
186192
return self._image
187193

188-
self._image = Image.open(BytesIO(self.data), formats=LIBRARY_SUPPORTED_FORMATS)
194+
try:
195+
self._image = Image.open(BytesIO(self.data), formats=LIBRARY_SUPPORTED_FORMATS)
196+
except UnidentifiedImageError:
197+
self._image = None
189198
self._image_decoded = True
190199
return self._image
191200

0 commit comments

Comments
 (0)