def array2image(a):
 if (a.dtype.kind in ['u', 'I', 'B']):
  mode = 'L'
 elif (a.dtype.kind == numpy.float32):
  mode = 'F'
 else:
  raise ValueError, 'unsupported image mode'
 try:
  im = Image.fromstring(mode, (a.shape[1], a.shape[0]), a.tostring())
 except Exception:
  im = Image.frombytes(mode, (a.shape[1], a.shape[0]), a.tostring())
 return im

def array2image ( a ) : if ( a . dtype . kind in [ 'u' , 'I' , 'B' ] ) : mode = 'L' elif ( a . dtype . kind == numpy . float32 ) : mode = 'F' else : raise ValueError , 'unsupported image mode' try : im = Image . fromstring ( mode , ( a . shape [ 1 ] , a . shape [ 0 ] ) , a . tostring ( ) ) except Exception : im = Image . frombytes ( mode , ( a . shape [ 1 ] , a . shape [ 0 ] ) , a . tostring ( ) ) return im
takes an array and returns an image object .