Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data] Raise error if PIL can't load image #38030

Merged
merged 4 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)


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