Skip to content

Commit

Permalink
[Data] Raise error if PIL can't load image (ray-project#38030)
Browse files Browse the repository at this point in the history
If you call read_images and PIL can't load a file, you get an unhelpful error message:

> PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x19997c350>

This PR updates the error message to include the path to the file
  • Loading branch information
bveeramani committed Aug 4, 2023
1 parent 332d1d3 commit 81b1e8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions python/ray/data/datasource/image_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ def _read_file(
include_paths: bool,
**reader_args,
) -> "pyarrow.Table":
from PIL import Image
from PIL import Image, UnidentifiedImageError

records = super()._read_file(f, path, include_paths=True, **reader_args)
assert len(records) == 1
path, data = records[0]

image = Image.open(io.BytesIO(data))
try:
image = Image.open(io.BytesIO(data))
except UnidentifiedImageError as e:
raise ValueError(f"PIL couldn't load image file at path '{path}'.") from e

if size is not None:
height, width = size
image = image.resize((width, height))
Expand Down
6 changes: 6 additions & 0 deletions python/ray/data/tests/test_image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import tempfile
from typing import Dict
from unittest.mock import ANY, patch

Expand Down Expand Up @@ -256,6 +257,11 @@ def test_args_passthrough(ray_start_regular_shared):
mock.assert_called_once_with(ANY, **kwargs)
assert isinstance(mock.call_args[0][0], ImageDatasource)

def test_unidentified_image_error(ray_start_regular_shared):
with tempfile.NamedTemporaryFile(suffix=".png") as file:
with pytest.raises(ValueError):
ray.data.read_images(paths=file.name).materialize()


if __name__ == "__main__":
import sys
Expand Down

0 comments on commit 81b1e8f

Please sign in to comment.