def img_to_array(img, dim_ordering='default'):
 if (dim_ordering == 'default'):
  dim_ordering = K.image_dim_ordering()
 if (dim_ordering not in {'th', 'tf'}):
  raise ValueError('Unknown dim_ordering: ', dim_ordering)
 x = np.asarray(img, dtype='float32')
 if (len(x.shape) == 3):
  if (dim_ordering == 'th'):
   x = x.transpose(2, 0, 1)
 elif (len(x.shape) == 2):
  if (dim_ordering == 'th'):
   x = x.reshape((1, x.shape[0], x.shape[1]))
  else:
   x = x.reshape((x.shape[0], x.shape[1], 1))
 else:
  raise ValueError('Unsupported image shape: ', x.shape)
 return x

def img_to_array ( img , dim_ordering = 'default' ) : if ( dim_ordering == 'default' ) : dim_ordering = K . image_dim_ordering ( ) if ( dim_ordering not in { 'th' , 'tf' } ) : raise ValueError ( 'Unknown dim_ordering: ' , dim_ordering ) x = np . asarray ( img , dtype = 'float32' ) if ( len ( x . shape ) == 3 ) : if ( dim_ordering == 'th' ) : x = x . transpose ( 2 , 0 , 1 ) elif ( len ( x . shape ) == 2 ) : if ( dim_ordering == 'th' ) : x = x . reshape ( ( 1 , x . shape [ 0 ] , x . shape [ 1 ] ) ) else : x = x . reshape ( ( x . shape [ 0 ] , x . shape [ 1 ] , 1 ) ) else : raise ValueError ( 'Unsupported image shape: ' , x . shape ) return x
converts a pil image instance to a numpy array .