@@ -354,20 +354,22 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
354354 out_height = int (out_height_base )
355355
356356 if not unsampled :
357+ created_rgba_mask = False
358+
357359 if A .ndim == 2 :
358360 A = self .norm (A )
361+ # If the image is greyscale, convert to RGBA with the
362+ # correct alpha channel for resizing
363+ rgba = np .empty ((A .shape [0 ], A .shape [1 ], 4 ), dtype = A .dtype )
364+ rgba [..., 0 :3 ] = np .expand_dims (A , 2 )
359365 if A .dtype .kind == 'f' :
360- # For floating-point greyscale images, we treat negative
361- # numbers as transparent.
362-
363- # TODO: Use np.full when we support Numpy 1.9 as a
364- # minimum
365- output = np .empty ((out_height , out_width ), dtype = A .dtype )
366- output [...] = - 100.0
366+ rgba [..., 3 ] = ~ A .mask
367367 else :
368- output = np .zeros ((out_height , out_width ), dtype = A .dtype )
369-
368+ rgba [..., 3 ] = np .where (A .mask , 0 , np .iinfo (A .dtype ).max )
369+ A = rgba
370+ output = np .zeros ((out_height , out_width , 4 ), dtype = A .dtype )
370371 alpha = 1.0
372+ created_rgba_mask = True
371373 elif A .ndim == 3 :
372374 # Always convert to RGBA, even if only RGB input
373375 if A .shape [2 ] == 3 :
@@ -388,10 +390,16 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
388390 self .get_resample (), alpha ,
389391 self .get_filternorm () or 0.0 , self .get_filterrad () or 0.0 )
390392
393+ if created_rgba_mask :
394+ # Convert back to a masked greyscale array so
395+ # colormapping works correctly
396+ output = np .ma .masked_array (
397+ output [..., 0 ], output [..., 3 ] < 0.5 )
398+
391399 output = self .to_rgba (output , bytes = True , norm = False )
392400
393- # Apply alpha *after* if the input was greyscale
394- if A .ndim == 2 :
401+ # Apply alpha *after* if the input was greyscale without a mask
402+ if A .ndim == 2 or created_rgba_mask :
395403 alpha = self .get_alpha ()
396404 if alpha is not None and alpha != 1.0 :
397405 alpha_channel = output [:, :, 3 ]
0 commit comments