Skip to content

Commit

Permalink
Fix broken ImageRescaleWrapper due to PIL's resize() change.
Browse files Browse the repository at this point in the history
PIL 10.4.0's `Image.resize()` behavior causes a breakage in
`ImageRescaleWrapper` due to the way numpy arrays are compared. In particular,
in 10.3.0 the `size` argument was cast to a `tuple`, but not in 10.4.0 (see
[changes](python-pillow/Pillow@10.3.0...10.4.0)).
This has since been
[reinstated](python-pillow/Pillow@936012e),
but it did not make it to the 10.4.0 release.

We can still change our code though, so we now simply pass a `tuple` instead of
an `np.array`.

PiperOrigin-RevId: 651877307
  • Loading branch information
kenjitoyama authored and copybara-github committed Jul 12, 2024
1 parent a2b9284 commit 35d5941
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions android_env/wrappers/image_rescale_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ def _process_pixels(self, raw_observation: np.ndarray) -> np.ndarray:
return self._resize_image_array(image, new_shape)

def _resize_image_array(
self,
grayscale_or_rbg_array: np.ndarray,
new_shape: Sequence[int]) -> np.ndarray:
self, grayscale_or_rbg_array: np.ndarray, new_shape: np.ndarray
) -> np.ndarray:
"""Resize color or grayscale/action_layer array to new_shape."""
assert np.array(new_shape).ndim == 1
assert new_shape.ndim == 1
assert len(new_shape) == 2
resized_array = np.array(Image.fromarray(
grayscale_or_rbg_array.astype('uint8')).resize(new_shape))
resized_array = np.array(
Image.fromarray(grayscale_or_rbg_array.astype('uint8')).resize(
tuple(new_shape)
)
)
if resized_array.ndim == 2:
return np.expand_dims(resized_array, axis=-1)
return resized_array
Expand Down

0 comments on commit 35d5941

Please sign in to comment.