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

Fix support for compressing mono images by respecting mode to determine depth #4847

Merged
merged 3 commits into from
Jan 19, 2024
Merged
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
9 changes: 8 additions & 1 deletion rerun_py/rerun_sdk/rerun/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,17 @@ def __init__(
tensor_buffer = TensorBuffer(np.frombuffer(np_buffer, dtype=np.uint8))
tensor_buffer.kind = "jpeg"

if img_data.mode == "L":
depth = 1
elif img_data.mode == "RGB":
depth = 3
else:
raise ValueError(f"Unsupported JPEG mode: {img_data.mode}")

tensor_shape = (
TensorDimension(img_data.height, "height"),
TensorDimension(img_data.width, "width"),
TensorDimension(3, "depth"),
TensorDimension(depth, "depth"),
)
tensor_data = TensorData(buffer=tensor_buffer, shape=tensor_shape)
else:
Expand Down
47 changes: 47 additions & 0 deletions rerun_py/tests/unit/test_image_encoded.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_image_encoded_png() -> None:

assert img.data.shape[0].size == 200
assert img.data.shape[1].size == 300
assert img.data.shape[2].size == 4
assert img.data.buffer.kind == "u8"


Expand All @@ -28,8 +29,26 @@ def test_image_encoded_jpg() -> None:

img = rr.ImageEncoded(path=file_path)

assert len(img.data.shape) == 3
assert img.data.shape[0].size == 200
assert img.data.shape[1].size == 300
assert img.data.shape[2].size == 3

assert img.data.buffer.kind == "jpeg"


def test_image_encoded_mono_jpg() -> None:
_, file_path = tempfile.mkstemp(suffix=".jpg")

image = Image.new("L", (300, 200), color=0)
image.save(file_path)

img = rr.ImageEncoded(path=file_path)

assert len(img.data.shape) == 3
assert img.data.shape[0].size == 200
assert img.data.shape[1].size == 300
assert img.data.shape[2].size == 1
assert img.data.buffer.kind == "jpeg"


Expand All @@ -41,13 +60,41 @@ def test_image_encoded_jpg_from_bytes() -> None:

img = rr.ImageEncoded(contents=bin)

assert len(img.data.shape) == 3
assert img.data.shape[0].size == 200
assert img.data.shape[1].size == 300
assert img.data.shape[2].size == 3
assert img.data.buffer.kind == "jpeg"

bin.seek(0)
img = rr.ImageEncoded(contents=bin.read())

assert len(img.data.shape) == 3
assert img.data.shape[0].size == 200
assert img.data.shape[1].size == 300
assert img.data.shape[2].size == 3
assert img.data.buffer.kind == "jpeg"


def test_image_encoded_mono_jpg_from_bytes() -> None:
bin = io.BytesIO()

image = Image.new("L", (300, 200), color=0)
image.save(bin, format="jpeg")

img = rr.ImageEncoded(contents=bin)

assert len(img.data.shape) == 3
assert img.data.shape[0].size == 200
assert img.data.shape[1].size == 300
assert img.data.shape[2].size == 1
assert img.data.buffer.kind == "jpeg"

bin.seek(0)
img = rr.ImageEncoded(contents=bin.read())

assert len(img.data.shape) == 3
assert img.data.shape[0].size == 200
assert img.data.shape[1].size == 300
assert img.data.shape[2].size == 1
assert img.data.buffer.kind == "jpeg"
Loading