How to create colour gradient in Python?

2019-01-31 14:33发布

I want to create a new colormap which interpolates between green and blue (or any other two colours for that matter). My goal is to get something like: gradient

First of all I am really not sure if this can be done using linear interpolation of blue and green. If it can, I'm not sure how to do so, I found some documentation on using a matplotlib method that interpolates specified RGB values here

The real trouble is understanding how "cdict2" works below. For the example the documentation says:

"Example: suppose you want red to increase from 0 to 1 over the bottom half, green to do the same over the middle half, and blue over the top half. Then you would use:"

from matplotlib import pyplot as plt
import matplotlib 
import numpy as np

plt.figure()
a=np.outer(np.arange(0,1,0.01),np.ones(10))
cdict2 = {'red':   [(0.0,  0.0, 0.0),
                   (0.5,  1.0, 1.0),
                   (1.0,  1.0, 1.0)],
         'green': [(0.0,  0.0, 0.0),
                   (0.25, 0.0, 0.0),
                   (0.75, 1.0, 1.0),
                   (1.0,  1.0, 1.0)],
         'blue':  [(0.0,  0.0, 0.0),
                   (0.5,  0.0, 0.0),
                   (1.0,  1.0, 1.0)]} 
my_cmap2 = matplotlib.colors.LinearSegmentedColormap('my_colormap2',cdict2,256)
plt.imshow(a,aspect='auto', cmap =my_cmap2)                   
plt.show()

EDIT: I now understand how the interpolation works, for example this will give a red to white interpolation:

White to red: Going down the columns of the "matrix" for each colour, in column one we have the xcoordinate of where we want the interpolation to start and end and the two other columns are the actual values for the colour value at that coordinate.

cdict2 = {'red':   [(0.0,  1.0, 1.0),
                    (1.0,  1.0, 1.0),
                    (1.0,  1.0, 1.0)],
         'green': [(0.0,  1.0, 1.0),
                   (1.0, 0.0, 0.0),
                   (1.0,  0.0, 0.0)],
     'blue':  [(0.0,  1.0, 1.0),
               (1.0,  0.0, 0.0),
               (1.0,  0.0, 0.0)]} 

It is evident that the gradient I want will be very difficult to create by interpolating in RGB space...

6条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-31 14:51

This creates a colormap controlled by a single parameter, y:

from matplotlib.colors import LinearSegmentedColormap


def bluegreen(y):
    red = [(0.0, 0.0, 0.0), (0.5, y, y), (1.0, 0.0, 0.0)]
    green = [(0.0, 0.0, 0.0), (0.5, y, y), (1.0, y, y)]
    blue = [(0.0, y, y), (0.5, y, y),(1.0,0.0,0.0)]
    colordict = dict(red=red, green=green, blue=blue)
    bluegreenmap = LinearSegmentedColormap('bluegreen', colordict, 256)
    return bluegreenmap

red ramps up from 0 to y and then back down to 0. green ramps up from 0 to y and then is constant. blue stars at y and is constant for the first half, then ramps down to 0.

Here's the plot with y = 0.7:

bluegreen color map

You could smooth it out by using adding another segment or two.

查看更多
Melony?
3楼-- · 2019-01-31 14:53

A simple answer I have not seen yet is to just use the colour package.

Install via pip

pip install colour

Use as so:

from colour import Color
red = Color("red")
colors = list(red.range_to(Color("green"),10))

# colors is now a list of length 10
# Containing: 
# [<Color red>, <Color #f13600>, <Color #e36500>, <Color #d58e00>, <Color #c7b000>, <Color #a4b800>, <Color #72aa00>, <Color #459c00>, <Color #208e00>, <Color green>]

Change the inputs to any colors you want

查看更多
乱世女痞
4楼-- · 2019-01-31 14:53

I needed this as well, but I wanted to enter multiple arbitrary color points. Consider a heat map where you need black, blue, green... all the way up to "hot" colors. I borrowed Mark Ransom's code above and extended it to meet my needs. I'm very happy with it. My thanks to all, especially Mark.

This code is neutral to the size of the image (no constants in the gaussian distribution); you can change it with the width= parameter to pixel(). It also allows tuning the "spread" (-> stddev) of the distribution; you can muddle them up further or introduce black bands by changing the spread= parameter to pixel().

#!/usr/bin/env python

import math
from PIL import Image
im = Image.new('RGB', (3000, 2000))
ld = im.load()

# A map of rgb points in your distribution
# [distance, (r, g, b)]
# distance is percentage from left edge
heatmap = [
    [0.0, (0, 0, 0)],
    [0.20, (0, 0, .5)],
    [0.40, (0, .5, 0)],
    [0.60, (.5, 0, 0)],
    [0.80, (.75, .75, 0)],
    [0.90, (1.0, .75, 0)],
    [1.00, (1.0, 1.0, 1.0)],
]

def gaussian(x, a, b, c, d=0):
    return a * math.exp(-(x - b)**2 / (2 * c**2)) + d

def pixel(x, width=100, map=[], spread=1):
    width = float(width)
    r = sum([gaussian(x, p[1][0], p[0] * width, width/(spread*len(map))) for p in map])
    g = sum([gaussian(x, p[1][1], p[0] * width, width/(spread*len(map))) for p in map])
    b = sum([gaussian(x, p[1][2], p[0] * width, width/(spread*len(map))) for p in map])
    return min(1.0, r), min(1.0, g), min(1.0, b)

for x in range(im.size[0]):
    r, g, b = pixel(x, width=3000, map=heatmap)
    r, g, b = [int(256*v) for v in (r, g, b)]
    for y in range(im.size[1]):
        ld[x, y] = r, g, b

im.save('grad.png')
查看更多
ら.Afraid
5楼-- · 2019-01-31 14:54

If you just need to interpolate in between 2 colors, I wrote a simple function for that. fadeColor creates you a hex color code out of two other hex color codes.

import matplotlib.pyplot as plt
import numpy as np

def fadeColor(c1,c2,mix=0): #fade (linear interpolate) from color c1 (at mix=0) to c2 (mix=1)
    assert len(c1)==len(c2)
    assert mix>=0 and mix<=1, 'mix='+str(mix)
    rgb1=np.array([int(c1[ii:ii+2],16) for ii in range(1,len(c1),2)])
    rgb2=np.array([int(c2[ii:ii+2],16) for ii in range(1,len(c2),2)])   
    rgb=((1-mix)*rgb1+mix*rgb2).astype(int)
    #cOld='#'+''.join([hex(a)[2:] for a in rgb])
    #print(11,[hex(a)[2:].zfill(2) for a in rgb])
    c='#'+('{:}'*3).format(*[hex(a)[2:].zfill(2) for a in rgb])
    #print(rgb1, rgb2, rgb, cOld, c)
    return c

fig, ax = plt.subplots(figsize=(8, 5))
c1='#1f77b4' #blue
c2='#2ca02c' #green

# another set of colors 
#c1='#ff0000' #red 
#c2='#001dff' #blue

n=500
for x in range(n+1):
    ax.axvline(x, color=fadeColor(c1,c2,x/n), linewidth=4) 

plt.show()

result:

enter image description here

查看更多
成全新的幸福
6楼-- · 2019-01-31 15:04

The first element of each tuple (0, 0.25, 0.5, etc) is the place where the color should be a certain value. I took 5 samples to see the RGB components (in GIMP), and placed them in the tables. The RGB components go from 0 to 1, so I had to divide them by 255.0 to scale the normal 0-255 values.

The 5 points are a rather coarse approximation - if you want a 'smoother' appearance, use more values.

from matplotlib import pyplot as plt
import matplotlib 
import numpy as np

plt.figure()
a=np.outer(np.arange(0,1,0.01),np.ones(10))
fact = 1.0/255.0
cdict2 = {'red':  [(0.0,   22*fact,  22*fact),
                   (0.25, 133*fact, 133*fact),
                   (0.5,  191*fact, 191*fact),
                   (0.75, 151*fact, 151*fact),
                   (1.0,   25*fact,  25*fact)],
         'green': [(0.0,   65*fact,  65*fact),
                   (0.25, 182*fact, 182*fact),
                   (0.5,  217*fact, 217*fact),
                   (0.75, 203*fact, 203*fact),
                   (1.0,   88*fact,  88*fact)],
         'blue':  [(0.0,  153*fact, 153*fact),
                   (0.25, 222*fact, 222*fact),
                   (0.5,  214*fact, 214*fact),
                   (0.75, 143*fact, 143*fact),
                   (1.0,   40*fact,  40*fact)]} 
my_cmap2 = matplotlib.colors.LinearSegmentedColormap('my_colormap2',cdict2,256)
plt.imshow(a,aspect='auto', cmap =my_cmap2)                   
plt.show()

Note that red is quite present. It's there because the center area approaches gray - where the three components are necessary.

This produces: result from the above table

查看更多
戒情不戒烟
7楼-- · 2019-01-31 15:08

It's obvious that your original example gradient is not linear. Have a look at a graph of the red, green, and blue values averaged across the image:

example gradient graph

Attempting to recreate this with a combination of linear gradients is going to be difficult.

To me each color looks like the addition of two gaussian curves, so I did some best fits and came up with this:

simulated

Using these calculated values lets me create a really pretty gradient that matches yours almost exactly.

import math
from PIL import Image
im = Image.new('RGB', (604, 62))
ld = im.load()

def gaussian(x, a, b, c, d=0):
    return a * math.exp(-(x - b)**2 / (2 * c**2)) + d

for x in range(im.size[0]):
    r = int(gaussian(x, 158.8242, 201, 87.0739) + gaussian(x, 158.8242, 402, 87.0739))
    g = int(gaussian(x, 129.9851, 157.7571, 108.0298) + gaussian(x, 200.6831, 399.4535, 143.6828))
    b = int(gaussian(x, 231.3135, 206.4774, 201.5447) + gaussian(x, 17.1017, 395.8819, 39.3148))
    for y in range(im.size[1]):
        ld[x, y] = (r, g, b)

recreated gradient

Unfortunately I don't yet know how to generalize it to arbitrary colors.

查看更多
登录 后发表回答