Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion packages/openpi-client/src/openpi_client/image_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ def convert_to_uint8(img: np.ndarray) -> np.ndarray:
"""Converts an image to uint8 if it is a float image.

This is important for reducing the size of the image when sending it over the network.

Float inputs are assumed to be in approximately the [0, 1] range. Values are
clipped before scaling so out-of-range pixels do not wrap under `astype(uint8)`.
"""
if np.issubdtype(img.dtype, np.floating):
img = (255 * img).astype(np.uint8)
img = (255 * np.clip(img, 0.0, 1.0)).astype(np.uint8)
return img


Expand Down
14 changes: 14 additions & 0 deletions packages/openpi-client/src/openpi_client/image_tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ def test_resize_with_pad_shapes():
resized_images = image_tools.resize_with_pad(images, height, width)
assert resized_images.shape == (1, height, width, 3)
assert np.all(resized_images == 0)


def test_convert_to_uint8_clips_out_of_range_floats():
img = np.array([[[-0.5, 0.0, 1.5]]], dtype=np.float32)
out = image_tools.convert_to_uint8(img)
assert out.dtype == np.uint8
assert out.tolist() == [[[0, 0, 255]]]


def test_convert_to_uint8_passes_through_uint8():
img = np.array([[[10, 20, 30]]], dtype=np.uint8)
out = image_tools.convert_to_uint8(img)
assert out.dtype == np.uint8
assert np.array_equal(out, img)