def cpt2seg(file_name, sym=False, discrete=False):
    """Reads a .cpt palette and returns a segmented colormap.

    sym : If True, the returned colormap contains the palette and a mirrored copy.
    For example, a blue-red-green palette would return a blue-red-green-green-red-blue colormap.

    discrete : If true, the returned colormap has a fixed number of uniform colors.
    That is, colors are not interpolated to form a continuous range.

    Example :
    >>> _palette_data = cpt2seg('palette.cpt')
    >>> palette = matplotlib.colors.LinearSegmentedColormap('palette', _palette_data, 100)
    >>> imshow(X, cmap=palette)
    """
   
   
    dic = {}
#    io
#    f = scipy.io.open(file_name, 'r')
#    rgb = f.read_array(f)
    
    #Check flags:
#    with open(file_name) as f:
#        content = f.readlines()
#        header=np.where(np.array([c.startswith('#') for c in content]))[0][-1]
#        footer=np.where(np.array([c[0].isalpha() for c in content]))[0][0]
    
    rgb = np.genfromtxt(file_name,comments='#',invalid_raise=False)
#    rgb = np.genfromtxt(file_name)
    rgb = rgb/255.
    s = shape(rgb)
    colors = ['red', 'green', 'blue']
    for c in colors:
        i = colors.index(c)
        x = rgb[:, i+1]

        if discrete:
            if sym:
                dic[c] = zeros((2*s[0]+1, 3), dtype=Float)
                dic[c][:,0] = linspace(0,1,2*s[0]+1)
                vec = concatenate((x ,x[::-1]))
            else:
                dic[c] = zeros((s[0]+1, 3), dtype=Float)
                dic[c][:,0] = linspace(0,1,s[0]+1)
                vec = x
            dic[c][1:, 1] = vec
            dic[c][:-1,2] = vec
               
        else:
            if sym:
                dic[c] = zeros((2*s[0], 3), dtype=Float)
                dic[c][:,0] = linspace(0,1,2*s[0])
                vec = concatenate((x ,x[::-1]))
            else:
                dic[c] = zeros((s[0], 3), dtype=Float)
                dic[c][:,0] = linspace(0,1,s[0])
                vec = x
            dic[c][:, 1] = vec
            dic[c][:, 2] = vec
   
    return dic
def cpt2seg ( file_name , sym = False , discrete = False ) : dic = { } rgb = np . genfromtxt ( file_name , comments = '#' , invalid_raise = False ) rgb = rgb / 255. s = shape ( rgb ) colors = [ 'red' , 'green' , 'blue' ] for c in colors : i = colors . index ( c ) x = rgb [ : , i + 1 ] if discrete : if sym : dic [ c ] = zeros ( ( 2 * s [ 0 ] + 1 , 3 ) , dtype = Float ) dic [ c ] [ : , 0 ] = linspace ( 0 , 1 , 2 * s [ 0 ] + 1 ) vec = concatenate ( ( x , x [ : : - 1 ] ) ) else : dic [ c ] = zeros ( ( s [ 0 ] + 1 , 3 ) , dtype = Float ) dic [ c ] [ : , 0 ] = linspace ( 0 , 1 , s [ 0 ] + 1 ) vec = x dic [ c ] [ 1 : , 1 ] = vec dic [ c ] [ : - 1 , 2 ] = vec else : if sym : dic [ c ] = zeros ( ( 2 * s [ 0 ] , 3 ) , dtype = Float ) dic [ c ] [ : , 0 ] = linspace ( 0 , 1 , 2 * s [ 0 ] ) vec = concatenate ( ( x , x [ : : - 1 ] ) ) else : dic [ c ] = zeros ( ( s [ 0 ] , 3 ) , dtype = Float ) dic [ c ] [ : , 0 ] = linspace ( 0 , 1 , s [ 0 ] ) vec = x dic [ c ] [ : , 1 ] = vec dic [ c ] [ : , 2 ] = vec return dic
Reads a .cpt palette and returns a segmented colormap.

    sym : If True, the returned colormap contains the palette and a mirrored copy.
    For example, a blue-red-green palette would return a blue-red-green-green-red-blue colormap.

    discrete : If true, the returned colormap has a fixed number of uniform colors.
    That is, colors are not interpolated to form a continuous range.

    Example :
    >>> _palette_data = cpt2seg('palette.cpt')
    >>> palette = matplotlib.colors.LinearSegmentedColormap('palette', _palette_data, 100)
    >>> imshow(X, cmap=palette)