Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3cdca5e

Browse filesBrowse files
author
Joan Martinez
committed
fix: changes for ImageBytes
1 parent a4419bb commit 3cdca5e
Copy full SHA for 3cdca5e

File tree

2 files changed

+26
-7
lines changed
Filter options

2 files changed

+26
-7
lines changed

‎docarray/typing/bytes/base_bytes.py

Copy file name to clipboardExpand all lines: docarray/typing/bytes/base_bytes.py
-6Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ def _to_node_protobuf(self: T) -> 'NodeProto':
5555

5656
return NodeProto(blob=self, type=self._proto_type_name)
5757

58-
def _docarray_to_json_compatible(self):
59-
"""
60-
Convert itself into a json compatible object
61-
"""
62-
return self.decode()
63-
6458
if is_pydantic_v2:
6559

6660
@classmethod

‎docarray/typing/bytes/image_bytes.py

Copy file name to clipboardExpand all lines: docarray/typing/bytes/image_bytes.py
+26-1Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from io import BytesIO
2-
from typing import TYPE_CHECKING, Optional, Tuple, TypeVar
2+
from typing import TYPE_CHECKING, Optional, Tuple, TypeVar, Type, Any
33

44
import numpy as np
55
from pydantic import parse_obj_as
66

7+
78
from docarray.typing.bytes.base_bytes import BaseBytes
89
from docarray.typing.proto_register import _register_proto
910
from docarray.typing.tensor.image.image_ndarray import ImageNdArray
1011
from docarray.utils._internal.misc import import_library
12+
from docarray.utils._internal.pydantic import bytes_validator
13+
1114

1215
if TYPE_CHECKING:
1316
from PIL import Image as PILImage
@@ -22,6 +25,28 @@ class ImageBytes(BaseBytes):
2225
Bytes that store an image and that can be load into an image tensor
2326
"""
2427

28+
@classmethod
29+
def _docarray_validate(
30+
cls: Type[T],
31+
value: Any,
32+
) -> T:
33+
if isinstance(value, str):
34+
import base64
35+
36+
return cls(base64.b64decode(value))
37+
else:
38+
value = bytes_validator(value)
39+
return cls(value)
40+
41+
def _docarray_to_json_compatible(self):
42+
"""
43+
Convert itself into a json compatible object
44+
"""
45+
import base64
46+
47+
encoded_str = base64.b64encode(self).decode('utf-8')
48+
return encoded_str
49+
2550
def load_pil(
2651
self,
2752
) -> 'PILImage.Image':

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.