Skip to content

Commit

Permalink
Support array-likes and literal strings in image conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
druzsan committed Oct 25, 2023
1 parent 723092a commit 63a2845
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions renumics/spotlight/dtypes/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,22 @@ def _(value: Union[str, np.str_], _: dtypes.DType) -> np.ndarray:
raise ConversionError("Cannot interpret string as a 1D sequence")


@convert("Image", simple=False)
def _(value: Union[np.ndarray, list], _: dtypes.DType) -> bytes:
return media.Image(value).encode().tolist()


@convert("Image", simple=False)
def _(value: Union[str, np.str_], _: dtypes.DType) -> bytes:
try:
if data := read_external_value(value, dtypes.image_dtype):
return data.tolist()
except InvalidFile:
raise ConversionError()
try:
obj = ast.literal_eval(value)
return media.Image(obj).encode().tolist()
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
raise ConversionError()
raise ConversionError()


Expand All @@ -352,11 +361,6 @@ def _(value: Union[bytes, np.bytes_], _: dtypes.DType) -> bytes:
return media.Image.from_bytes(value).encode().tolist()


@convert("Image", simple=False)
def _(value: np.ndarray, _: dtypes.DType) -> bytes:
return media.Image(value).encode().tolist()


@convert("Image", simple=False)
def _(value: PIL.Image.Image, _: dtypes.DType) -> bytes:
buffer = io.BytesIO()
Expand Down Expand Up @@ -422,7 +426,7 @@ def _(_: Union[np.ndarray, list, str, np.str_], _dtype: dtypes.DType) -> str:


@convert("Image", simple=True)
def _(_: np.ndarray, _dtype: dtypes.DType) -> str:
def _(_: Union[np.ndarray, list], _dtype: dtypes.DType) -> str:
return "[...]"


Expand Down

0 comments on commit 63a2845

Please sign in to comment.