55"""
66from __future__ import division , print_function
77import os , warnings
8+ import math
89
910import numpy as np
1011from numpy import ma
@@ -325,7 +326,7 @@ def _draw_unsampled_image(self, renderer, gc):
325326
326327 im ._url = self .get_url ()
327328 im ._gid = self .get_gid ()
328-
329+
329330 renderer .draw_image (gc , xmin , ymin , im , dxintv , dyintv ,
330331 trans_ic_to_canvas )
331332
@@ -1184,6 +1185,9 @@ def pilread():
11841185 if cbook .is_string_like (fname ):
11851186 basename , ext = os .path .splitext (fname )
11861187 ext = ext .lower ()[1 :]
1188+ elif hasattr (fname , 'name' ):
1189+ basename , ext = os .path .splitext (fname .name )
1190+ ext = ext .lower ()[1 :]
11871191 else :
11881192 ext = 'png'
11891193 else :
@@ -1251,14 +1255,14 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
12511255
12521256def pil_to_array ( pilImage ):
12531257 """
1254- load a PIL image and return it as a numpy array of uint8 . For
1255- grayscale images, the return array is MxN. For RGB images, the
1256- return value is MxNx3. For RGBA images the return value is MxNx4
1258+ Load a PIL image and return it as a numpy array. For grayscale
1259+ images, the return array is MxN. For RGB images, the return value
1260+ is MxNx3. For RGBA images the return value is MxNx4
12571261 """
1258- def toarray (im ):
1259- """Teturn a 1D array of floats ."""
1260- x_str = im .tostring ('raw' ,im .mode , 0 , - 1 )
1261- x = np .fromstring (x_str ,np . uint8 )
1262+ def toarray (im , dtype = np . uint8 ):
1263+ """Teturn a 1D array of dtype ."""
1264+ x_str = im .tostring ('raw' , im .mode )
1265+ x = np .fromstring (x_str , dtype )
12621266 return x
12631267
12641268 if pilImage .mode in ('RGBA' , 'RGBX' ):
@@ -1275,7 +1279,15 @@ def toarray(im):
12751279 x = toarray (im )
12761280 x .shape = im .size [1 ], im .size [0 ], 3
12771281 return x
1278-
1282+ elif pilImage .mode .startswith ('I;16' ):
1283+ # return MxN luminance array of uint16
1284+ im = pilImage
1285+ if im .mode .endswith ('B' ):
1286+ x = toarray (im , '>u2' )
1287+ else :
1288+ x = toarray (im , '<u2' )
1289+ x .shape = im .size [1 ], im .size [0 ]
1290+ return x .astype ('=u2' )
12791291 else : # try to convert to an rgba image
12801292 try :
12811293 im = pilImage .convert ('RGBA' )
0 commit comments