Skip to content

Commit 9a8282a

Browse files
committed
Fix ImageEncoded tests
1 parent fe4d89b commit 9a8282a

File tree

5 files changed

+108
-35
lines changed

5 files changed

+108
-35
lines changed

rerun_py/rerun_sdk/rerun/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
)
7777
from .components import (
7878
AlbedoFactor as AlbedoFactor,
79-
ImageFormat as ImageFormat,
8079
MediaType as MediaType,
8180
Radius as Radius,
8281
Scale3D as Scale3D,

rerun_py/rerun_sdk/rerun/_image_encoded.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,18 @@ def ImageEncoded(
202202

203203
media_type = None
204204
if format is not None:
205-
if str(format) == "BMP":
205+
if str(format).upper() == "BMP":
206206
media_type = "image/bmp"
207-
elif str(format) == "GIF":
207+
elif str(format).upper() == "GIF":
208208
media_type = "image/gif"
209-
elif str(format) == "JPEG":
209+
elif str(format).upper() == "JPEG":
210210
media_type = "image/jpeg"
211-
elif str(format) == "PNG":
211+
elif str(format).upper() == "PNG":
212212
media_type = "image/png"
213-
elif str(format) == "TIFF":
213+
elif str(format).upper() == "TIFF":
214214
media_type = "image/tiff"
215+
else:
216+
raise ValueError(f"Unknown image format: {format}")
215217

216218
if path is not None:
217219
return EncodedImage(

rerun_py/rerun_sdk/rerun/archetypes/encoded_image_ext.py

+5
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,16 @@ def __init__(
9393

9494
if path is None:
9595
blob = contents
96+
97+
if media_type is None:
98+
raise ValueError("Must provide 'media_type' when 'contents' is provided")
9699
else:
97100
blob = pathlib.Path(path).read_bytes()
101+
98102
if media_type is None:
99103
media_type = guess_media_type(str(path))
100104

105+
print("Making an encoded image with {}", media_type)
101106
self.__attrs_init__(blob=blob, media_type=media_type, draw_order=draw_order, opacity=opacity)
102107
return
103108

rerun_py/rerun_sdk/rerun/datatypes/blob_ext.py

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def native_to_pa_array_override(data: BlobArrayLike, data_type: pa.DataType) ->
4545
elif isinstance(data, bytes):
4646
inners = [pa.array(np.frombuffer(data, dtype=np.uint8))]
4747

48+
elif hasattr(data, "read"):
49+
inners = [pa.array(np.frombuffer(data.read(), dtype=np.uint8))]
50+
4851
# sequences
4952
elif isinstance(data, Sequence):
5053
if len(data) == 0:

rerun_py/tests/unit/test_image_encoded.py

+93-29
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,63 @@
77

88
import cv2
99
import numpy as np
10+
import pytest
1011
import rerun as rr # pip install rerun-sdk
1112
from PIL import Image
1213

1314

1415
def test_image_encoded_png() -> None:
15-
_, file_path = tempfile.mkstemp(suffix=".png")
16+
with tempfile.NamedTemporaryFile(suffix=".png") as tmp:
17+
file_path = tmp.name
1618

17-
image = Image.new("RGBA", (300, 200), color=(0, 0, 0, 0))
18-
image.save(file_path)
19+
image = Image.new("RGBA", (300, 200), color=(0, 0, 0, 0))
20+
image.save(file_path)
1921

20-
img = rr.ImageEncoded(path=file_path)
22+
with pytest.warns(DeprecationWarning) as warnings:
23+
img = rr.ImageEncoded(path=file_path)
24+
assert len(warnings) == 1
2125

22-
assert img.media_type == "image/png"
26+
assert type(img) is rr.EncodedImage
27+
assert img.media_type is not None
28+
media_type_arrow = img.media_type.as_arrow_array().storage[0].as_py()
29+
30+
assert media_type_arrow == "image/png"
2331

2432

2533
def test_image_encoded_jpg() -> None:
26-
_, file_path = tempfile.mkstemp(suffix=".jpg")
34+
with tempfile.NamedTemporaryFile(suffix=".jpg") as tmp:
35+
file_path = tmp.name
2736

28-
image = Image.new("RGB", (300, 200), color=(0, 0, 0))
29-
image.save(file_path)
37+
image = Image.new("RGB", (300, 200), color=(0, 0, 0))
38+
image.save(file_path)
3039

31-
img = rr.ImageEncoded(path=file_path)
40+
with pytest.warns(DeprecationWarning) as warnings:
41+
img = rr.ImageEncoded(path=file_path)
42+
assert len(warnings) == 1
3243

33-
assert img.media_type == "image/jpeg"
44+
assert type(img) is rr.EncodedImage
45+
assert img.media_type is not None
46+
media_type_arrow = img.media_type.as_arrow_array().storage[0].as_py()
47+
48+
assert media_type_arrow == "image/jpeg"
3449

3550

3651
def test_image_encoded_mono_jpg() -> None:
37-
_, file_path = tempfile.mkstemp(suffix=".jpg")
52+
with tempfile.NamedTemporaryFile(suffix=".jpeg") as tmp:
53+
file_path = tmp.name
3854

39-
image = Image.new("L", (300, 200), color=0)
40-
image.save(file_path)
55+
image = Image.new("L", (300, 200), color=0)
56+
image.save(file_path)
4157

42-
img = rr.ImageEncoded(path=file_path)
58+
with pytest.warns(DeprecationWarning) as warnings:
59+
img = rr.ImageEncoded(path=file_path)
60+
assert len(warnings) == 1
4361

44-
assert img.media_type == "image/jpeg"
62+
assert type(img) is rr.EncodedImage
63+
assert img.media_type is not None
64+
media_type_arrow = img.media_type.as_arrow_array().storage[0].as_py()
65+
66+
assert media_type_arrow == "image/jpeg"
4567

4668

4769
def test_image_encoded_jpg_from_bytes() -> None:
@@ -50,14 +72,27 @@ def test_image_encoded_jpg_from_bytes() -> None:
5072
image = Image.new("RGB", (300, 200), color=(0, 0, 0))
5173
image.save(bin, format="jpeg")
5274

53-
img = rr.ImageEncoded(contents=bin)
75+
with pytest.warns(DeprecationWarning) as warnings:
76+
img = rr.ImageEncoded(contents=bin, format=rr.ImageFormat.JPEG)
77+
assert len(warnings) == 1
78+
79+
assert type(img) is rr.EncodedImage
80+
assert img.media_type is not None
81+
media_type_arrow = img.media_type.as_arrow_array().storage[0].as_py()
5482

55-
assert img.media_type == "image/jpeg"
83+
assert media_type_arrow == "image/jpeg"
5684

5785
bin.seek(0)
58-
img = rr.ImageEncoded(contents=bin.read())
5986

60-
assert img.media_type == "image/jpeg"
87+
with pytest.warns(DeprecationWarning) as warnings:
88+
img = rr.ImageEncoded(contents=bin.read(), format=rr.ImageFormat.JPEG)
89+
assert len(warnings) == 1
90+
91+
assert type(img) is rr.EncodedImage
92+
assert img.media_type is not None
93+
media_type_arrow = img.media_type.as_arrow_array().storage[0].as_py()
94+
95+
assert media_type_arrow == "image/jpeg"
6196

6297

6398
def test_image_encoded_mono_jpg_from_bytes() -> None:
@@ -66,14 +101,27 @@ def test_image_encoded_mono_jpg_from_bytes() -> None:
66101
image = Image.new("L", (300, 200), color=0)
67102
image.save(bin, format="jpeg")
68103

69-
img = rr.ImageEncoded(contents=bin)
104+
with pytest.warns(DeprecationWarning) as warnings:
105+
img = rr.ImageEncoded(contents=bin, format=rr.ImageFormat.JPEG)
106+
assert len(warnings) == 1
70107

71-
assert img.media_type == "image/jpeg"
108+
assert type(img) is rr.EncodedImage
109+
assert img.media_type is not None
110+
media_type_arrow = img.media_type.as_arrow_array().storage[0].as_py()
111+
112+
assert media_type_arrow == "image/jpeg"
72113

73114
bin.seek(0)
74-
img = rr.ImageEncoded(contents=bin.read())
75115

76-
assert img.media_type == "image/jpeg"
116+
with pytest.warns(DeprecationWarning) as warnings:
117+
img = rr.ImageEncoded(contents=bin.read(), format=rr.ImageFormat.JPEG)
118+
assert len(warnings) == 1
119+
120+
assert type(img) is rr.EncodedImage
121+
assert img.media_type is not None
122+
media_type_arrow = img.media_type.as_arrow_array().storage[0].as_py()
123+
124+
assert media_type_arrow == "image/jpeg"
77125

78126

79127
def test_image_encoded_nv12() -> None:
@@ -86,14 +134,30 @@ def bgr2nv12(bgr: cv2.typing.MatLike) -> cv2.typing.MatLike:
86134

87135
img_bgr = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
88136

89-
img = (
90-
rr.ImageEncoded(
137+
with pytest.warns(DeprecationWarning) as warnings:
138+
img = rr.ImageEncoded(
91139
contents=bytes(bgr2nv12(img_bgr)),
92140
format=rr.ImageFormat.NV12((480, 640)),
93141
draw_order=42,
94-
),
142+
)
143+
assert len(warnings) == 1
144+
145+
assert type(img) is rr.Image
146+
147+
image_format_arrow = img.format.as_arrow_array().storage[0].as_py()
148+
149+
image_format = rr.components.ImageFormat(
150+
width=image_format_arrow["width"],
151+
height=image_format_arrow["height"],
152+
pixel_format=image_format_arrow["pixel_format"],
153+
channel_datatype=image_format_arrow["channel_datatype"],
154+
color_model=image_format_arrow["color_model"],
95155
)
96156

97-
assert img.resolution == rr.Resolution2D(640, 480)
98-
assert img.pixel_format == rr.PixelFormat.NV12
99-
assert img.draw_order == 42
157+
assert image_format.width == 640
158+
assert image_format.height == 480
159+
assert image_format.pixel_format == rr.PixelFormat.NV12
160+
161+
assert img.draw_order is not None
162+
draw_order_arrow = img.draw_order.as_arrow_array().storage[0].as_py()
163+
assert draw_order_arrow == 42

0 commit comments

Comments
 (0)