Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 11 additions & 2 deletions src/transformers/image_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def to_pil_image(
The image to convert to the `PIL.Image` format.
do_rescale (`bool`, *optional*):
Whether or not to apply the scaling factor (to make pixel values integers between 0 and 255). Will default
to `True` if the image type is a floating type, `False` otherwise.
to `True` if the image type is a floating type and casting to `int` would result in a loss of precision,
and `False` otherwise.

Returns:
`PIL.Image.Image`: The converted image.
Expand All @@ -156,9 +157,17 @@ def to_pil_image(
image = np.squeeze(image, axis=-1) if image.shape[-1] == 1 else image

# PIL.Image can only store uint8 values, so we rescale the image to be between 0 and 255 if needed.
do_rescale = isinstance(image.flat[0], (float, np.float32, np.float64)) if do_rescale is None else do_rescale
if do_rescale is None:
do_rescale = isinstance(image.flat[0], (float, np.floating)) and not np.allclose(image, image.astype(int))

@ydshieh ydshieh Mar 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would prefer to:

  • check if image in the range [0, 1]
    • yes: rescale
    • no: check if np.allclose(image, image.astype(int))
      • yes: not rescale
      • no: raise value error (before doing rescale)

But I am OK with this current version - I just feel it's a bit difficult to reason the logic (but not really from this PR)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed - that's tidier. I'll make this change 👍


if do_rescale:
image = rescale(image, 255)

if np.any(image < 0) or np.any(image > 255):
raise ValueError(
"The image to be converted to a PIL image contains values outside the range [0, 255], "
f"got [{image.min()}, {image.max()}] which cannot be converted to uint8."
)
image = image.astype(np.uint8)
return PIL.Image.fromarray(image)

Expand Down
5 changes: 5 additions & 0 deletions tests/test_image_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def test_to_pil_image_from_float(self, name, image_shape, dtype):
# make sure image is correctly rescaled
self.assertTrue(np.abs(np.asarray(pil_image)).sum() > 0)

# Make sure that an exception is raised if image is not in [0, 1]
image = np.random.randn(*image_shape).astype(dtype)
with self.assertRaises(ValueError):
to_pil_image(image)

@require_tf
def test_to_pil_image_from_tensorflow(self):
# channels_first
Expand Down