def image2array(im):
 if (im.mode not in ('L', 'F')):
  raise ValueError, 'can only convert single-layer images'
 try:
  imdata = im.tostring()
 except Exception:
  imdata = im.tobytes()
 if (im.mode == 'L'):
  a = numpy.fromstring(imdata, numpy.uint8)
 else:
  a = numpy.fromstring(imdata, numpy.float32)
 a.shape = (im.size[1], im.size[0])
 return a

def image2array ( im ) : if ( im . mode not in ( 'L' , 'F' ) ) : raise ValueError , 'can only convert single-layer images' try : imdata = im . tostring ( ) except Exception : imdata = im . tobytes ( ) if ( im . mode == 'L' ) : a = numpy . fromstring ( imdata , numpy . uint8 ) else : a = numpy . fromstring ( imdata , numpy . float32 ) a . shape = ( im . size [ 1 ] , im . size [ 0 ] ) return a
takes an image object and returns a numpy array .