7
7
8
8
import cv2
9
9
import numpy as np
10
+ import pytest
10
11
import rerun as rr # pip install rerun-sdk
11
12
from PIL import Image
12
13
13
14
14
15
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
16
18
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 )
19
21
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
21
25
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"
23
31
24
32
25
33
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
27
36
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 )
30
39
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
32
43
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"
34
49
35
50
36
51
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
38
54
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 )
41
57
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
43
61
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"
45
67
46
68
47
69
def test_image_encoded_jpg_from_bytes () -> None :
@@ -50,14 +72,27 @@ def test_image_encoded_jpg_from_bytes() -> None:
50
72
image = Image .new ("RGB" , (300 , 200 ), color = (0 , 0 , 0 ))
51
73
image .save (bin , format = "jpeg" )
52
74
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 ()
54
82
55
- assert img . media_type == "image/jpeg"
83
+ assert media_type_arrow == "image/jpeg"
56
84
57
85
bin .seek (0 )
58
- img = rr .ImageEncoded (contents = bin .read ())
59
86
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"
61
96
62
97
63
98
def test_image_encoded_mono_jpg_from_bytes () -> None :
@@ -66,14 +101,27 @@ def test_image_encoded_mono_jpg_from_bytes() -> None:
66
101
image = Image .new ("L" , (300 , 200 ), color = 0 )
67
102
image .save (bin , format = "jpeg" )
68
103
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
70
107
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"
72
113
73
114
bin .seek (0 )
74
- img = rr .ImageEncoded (contents = bin .read ())
75
115
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"
77
125
78
126
79
127
def test_image_encoded_nv12 () -> None :
@@ -86,14 +134,30 @@ def bgr2nv12(bgr: cv2.typing.MatLike) -> cv2.typing.MatLike:
86
134
87
135
img_bgr = np .random .randint (0 , 255 , (480 , 640 , 3 ), dtype = np .uint8 )
88
136
89
- img = (
90
- rr .ImageEncoded (
137
+ with pytest . warns ( DeprecationWarning ) as warnings :
138
+ img = rr .ImageEncoded (
91
139
contents = bytes (bgr2nv12 (img_bgr )),
92
140
format = rr .ImageFormat .NV12 ((480 , 640 )),
93
141
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" ],
95
155
)
96
156
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