python渐变色代码_如何在Python中创建颜色渐变?

6 个答案:

答案 0 :(得分:54)

我还没有看到一个简单的答案就是使用colour package。

通过pip安装

pip install colour

如此使用:

from colour import Color

red = Color("red")

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

# colors is now a list of length 10

# Containing:

# [, , , , , , , , , ]

将输入更改为您想要的任何颜色

答案 1 :(得分:26)

很明显,您的原始示例渐变不是线性的。查看图像中平均红色,绿色和蓝色值的图表:

AZsXg.png

尝试使用线性渐变的组合来重新创建它将是困难的。

对我来说,每种颜色看起来都像是两条高斯曲线的加法,所以我做了一些最佳配合,并提出了这个:

miCuQ.png

使用这些计算出的值,我可以创建一个非常漂亮的渐变,几乎完全匹配你的。

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)

NJkIQ.png

不幸的是,我还不知道如何将其推广为任意颜色。

答案 2 :(得分:6)

如果你只需要插入2种颜色,我就为此编写了一个简单的函数。 colorFader使用其他两个十六进制颜色代码创建一个十六进制颜色代码。

import matplotlib as mpl

import matplotlib.pyplot as plt

import numpy as np

def colorFader(c1,c2,mix=0): #fade (linear interpolate) from color c1 (at mix=0) to c2 (mix=1)

c1=np.array(mpl.colors.to_rgb(c1))

c2=np.array(mpl.colors.to_rgb(c2))

return mpl.colors.to_hex((1-mix)*c1 + mix*c2)

c1='#1f77b4' #blue

c2='green' #green

n=500

fig, ax = plt.subplots(figsize=(8, 5))

for x in range(n+1):

ax.axvline(x, color=colorFader(c1,c2,x/n), linewidth=4)

plt.show()

结果:强>

g7Lot.png

由于高兴趣而更新:

colorFader现在适用于rgb-colors和颜色字符串,如“red”或“r”。

答案 3 :(得分:6)

每个元组的第一个元素(0,0.25,0.5等)是颜色应该是某个值的位置。我拿了5个样本来查看RGB组件(在GIMP中),并将它们放在表格中。 RGB分量从0变为1,因此我必须将它们除以255.0以缩放正常的0-255值。

5分是一个相当粗略的近似值 - 如果你想要一个更平滑的'外观,使用更多的价值观。

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()

请注意,红色很明显。它就在那里因为中心区域接近灰色 - 这三个组成部分是必要的。

这会产生:

gK3aW.png

答案 4 :(得分:5)

这会创建一个由单个参数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从0上升到y,然后回到0. green从0上升到y然后是常量。 blue的{​​{1}}星在y处出现,并且在上半场保持不变,然后降至0。

这里是y = 0.7:的情节

NvFct.png

您可以使用添加另一个或两个段来平滑它。

答案 5 :(得分:4)

我也需要这个,但我想输入多个任意颜色点。考虑一个热图,你需要黑色,蓝色,绿色...一直到#34;热"颜色。我借用了Mark Ransom上面的代码并将其扩展以满足我的需求。我很满意。我要感谢所有人,尤其是马克。

此代码与图像大小无关(高斯分布中没有常数);您可以使用width =参数将其更改为pixel()。它还允许调整"传播" ( - > stddev)的分布;你可以通过将spread =参数更改为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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值