Skip to content

Commit

Permalink
Merge pull request #43 from Photoroom/ben/skip_deserialize_empty_image
Browse files Browse the repository at this point in the history
[Another nit] Opening an empty image would crash in python space
  • Loading branch information
blefaudeux authored Nov 18, 2024
2 parents 67103d3 + fee8c09 commit 4bec070
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion python/go_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@


def uint8_array_to_numpy(go_array):
if len(go_array.Data) == 0:
return None

# By convention, arrays which are already serialized as jpg or png are not reshaped
# We export them from Go with a Channels dimension of -1 to mark them as dimensionless.
# Anything else is a valid number of channels and will thus lead to a reshape
Expand All @@ -29,6 +32,9 @@ def uint8_array_to_numpy(go_array):


def go_array_to_numpy(go_array) -> Optional[np.ndarray]:
if len(go_array.Data) == 0:
return None

# Generic numpy-serialized array
bytes_buffer = bytes(go.Slice_byte(go_array.Data))
try:
Expand All @@ -39,7 +45,10 @@ def go_array_to_numpy(go_array) -> Optional[np.ndarray]:
return None


def go_array_to_pil_image(go_array):
def go_array_to_pil_image(go_array) -> Optional[Image.Image]:
if len(go_array.Data) == 0:
return None

# Zero copy conversion of the image buffer from Go to PIL.Image
np_array = uint8_array_to_numpy(go_array)
if go_array.Channels <= 0:
Expand Down
10 changes: 10 additions & 0 deletions python/test_datago_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ def test_has_tags():
assert "v4_trainset_hq" in sample.Tags, "v4_trainset_hq should be in the tags"


def test_empty_image():
client_config = get_json_config()
client_config["source_config"]["require_images"] = False

dataset = get_dataset(client_config)

# Just check that accessing the sample in python does not crash
_ = next(iter(dataset))


def no_test_jpg_compression():
# Check that the images are compressed as expected
client_config = get_json_config()
Expand Down

0 comments on commit 4bec070

Please sign in to comment.