def ndarray_from_pil(pil, dtype='uint8'):
 rval = np.asarray(pil)
 if (dtype != rval.dtype):
  rval = np.cast[dtype](rval)
 if str(dtype).startswith('float'):
  rval /= 255.0
 if (len(rval.shape) == 2):
  rval = rval.reshape(rval.shape[0], rval.shape[1], 1)
 return rval

def ndarray_from_pil ( pil , dtype = 'uint8' ) : rval = np . asarray ( pil ) if ( dtype != rval . dtype ) : rval = np . cast [ dtype ] ( rval ) if str ( dtype ) . startswith ( 'float' ) : rval /= 255.0 if ( len ( rval . shape ) == 2 ) : rval = rval . reshape ( rval . shape [ 0 ] , rval . shape [ 1 ] , 1 ) return rval
converts a pil image to an ndarray .