用cmap转换图像上的颜色

时间:2015-08-07 12:13:11

标签: python matplotlib colors python-imaging-library

前一段时间我写了这个脚本,用pil和colorsys转换颜色:

  # pd[x, y][3] = Alpha Code van de current Pixel. Alpha = 0, dan is pixel transparant.
        if pd[x, y][3] == 0:
            continue
        #label
    if pd[x,y][0] < 10 and pd[x,y][1] < 10 and pd[x,y][2] < 10:
        pd[x,y] = (0,0,0,0)
    if pd[x,y][0] > 250 and pd[x,y][1] > 250 and pd[x,y][2] > 250:
        pd[x,y] = (0,0,0,0)

        # A is niet 0, 1 maar 0 tot 255.
    #middle high precip
    if pd[x,y][1] == 255 and pd[x,y][0] > 100:
        pd[x,y] = (255,127,1,240)
        if pd[x, y][2] == 255:
            #low intense
            pd[x, y] = (10,100,0,240)
    #high precip
    if pd[x,y][0] > 190 and pd[x,y][1] > 150 and pd[x,y][2] == 0:
        pd[x,y] = (252,6,1,240)

    #somewhat higher then middle but lower than middle high precip
    if pd[x,y][1] == 255 and pd[x,y][0] < 55 and pd[x,y][2] < 155:
        pd[x,y] = (250,250,0,240)
#middle intense
    if pd[x,y][1] >= 75 and pd[x,y][2] >= 100:
        pd[x,y] = (5,200,5,240)
#Extreme precip
    if pd[x,y][0] >= 150 and pd[x,y][1] < 150 and pd[x,y][2] == 0:
        pd[x,y] = (255,0,255,240)

当时它取得了不错的成绩,但现在我想让它变得更加流畅。在我的其他一些剧本中,我得到了这个:

def make_colormap(seq):
    """Return a LinearSegmentedColormap
    seq: a sequence of floats and RGB-tuples. The floats should be increasing
    and in the interval (0,1).
    """
    seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
    cdict = {'red': [], 'green': [], 'blue': []}
    for i, item in enumerate(seq):
        if isinstance(item, float):
            r1, g1, b1 = seq[i - 1]
            r2, g2, b2 = seq[i + 1]
            cdict['red'].append([item, r1, r2])
            cdict['green'].append([item, g1, g2])
            cdict['blue'].append([item, b1, b2])
    return mcolors.LinearSegmentedColormap('CustomMap', cdict)
c = mcolors.ColorConverter().to_rgb
rvb = make_colormap(
    [c('blue') , c('cyan'), 0.2 ,c('green'), c('yellow'), 0.4, c('orange'), c('red'), 0.8 , c('darkred'), c('violet')])

我可以使用CMAP将原始脚本的颜色转换为CMap中使用的颜色,我该怎么做?提前谢谢!

0 个答案:

没有答案