@@ -115,3 +115,46 @@ def test_rendering():
115115 assert data_converted [:, 2 , 2 ].tolist () == [100 , 100 , 100 , 50 ]
116116 # Non-masked from CMAP
117117 assert data_converted [:, 3 , 3 ].tolist () == [255 , 255 , 255 , 255 ]
118+
119+
120+ def test_rendering_auto_dtype ():
121+ """Test Automatic format selection and dtype"""
122+ data = numpy .ma .zeros ((1 , 5 , 5 ), dtype = "uint16" ) + 1
123+ data .mask = False
124+ # add a masked value
125+ data .mask [0 , 0 , 0 ] = True
126+ im = ImageData (data )
127+ with pytest .warns (InvalidDatatypeWarning ):
128+ content , media = render_image (im )
129+ assert media == "image/png"
130+
131+ with MemoryFile (content ) as mem :
132+ with mem .open () as dst :
133+ assert dst .count == 2
134+ assert dst .dtypes == ("uint8" , "uint8" )
135+
136+ # Not Masked
137+ data = numpy .ma .zeros ((1 , 5 , 5 ), dtype = "uint16" ) + 1
138+ data .mask = False
139+ im = ImageData (data )
140+ with pytest .warns (InvalidDatatypeWarning ):
141+ content , media = render_image (im )
142+ assert media == "image/jpeg"
143+
144+ with MemoryFile (content ) as mem :
145+ with mem .open () as dst :
146+ assert dst .count == 1
147+ assert dst .dtypes == ("uint8" ,)
148+
149+ # Full Masked
150+ data = numpy .ma .zeros ((1 , 5 , 5 ), dtype = "uint16" ) + 1
151+ data .mask = True
152+ im = ImageData (data )
153+ with pytest .warns (InvalidDatatypeWarning ):
154+ content , media = render_image (im )
155+ assert media == "image/png"
156+
157+ with MemoryFile (content ) as mem :
158+ with mem .open () as dst :
159+ assert dst .count == 2
160+ assert dst .dtypes == ("uint8" , "uint8" )
0 commit comments