【问题标题】:Generating color gradient in Python在 Python 中生成颜色渐变
【发布时间】:2013-12-14 17:17:43
【问题描述】:

我有一个 RGB 颜色列表,需要在 python 中绘制它们之间的渐变。您对如何使用 PIL 库有什么建议吗?

编辑: 我明白了:

def gradient(list_of_colors):
    width = 600
    height = 480
    img = Image.new("RGB", (width, height))
    draw = ImageDraw.Draw(img)

    for i in range(len(list_of_colors)):
        r1,g1,b1 = list_of_colors[i]
        for x in range(width/len(list_of_colors)):
            colour = (r1,g1,b1)
            draw.line((x+(width/len(list_of_colors)*i), 0, x+(width/len(list_of_colors)*i), height), fill=colour)

    img.show()

gradient([(30, 198, 244), (99, 200, 72),(120, 50, 80),(200, 90, 140)])

它吸引了我: http://img59.imageshack.us/img59/1852/3gba.png

我只需要在这些颜色而不是颜色条纹之间进行渐变。 (类似这样)http://www.kees-tm.nl/uploads/colorgradient.jpg

【问题讨论】:

标签: python colors python-imaging-library draw gradient


【解决方案1】:

我认为这样的代码会起作用,它使用Linear Interpolation 来创建渐变。

list_of_colors = [(30, 198, 244), (99, 200, 72),(120, 50, 80),(200, 90, 140)]

no_steps = 100

def LerpColour(c1,c2,t):
    return (c1[0]+(c2[0]-c1[0])*t,c1[1]+(c2[1]-c1[1])*t,c1[2]+(c2[2]-c1[2])*t)

for i in range(len(list_of_colors)-2):
    for j in range(no_steps):
        colour = LerpColour(list_of_colors[i],list_of_colors[i+1],j/no_steps)

显然我不知道你是如何绘制渐变的,所以我把它留给你,用颜色变量做你喜欢的事情,在 for 循环中绘制渐变的每一步。 :)

另外:我不了解列表生成,所以如果有人可以改进 LerpColour 功能以使用它,请编辑我的帖子:)

编辑 - 使用 PIL 绘制时生成可以轻松迭代的列表:

gradient = []
for i in range(len(list_of_colors)-2):
    for j in range(no_steps):
        gradient.append(LerpColour(list_of_colors[i],list_of_colors[i+1],j/no_steps))

【讨论】:

  • 应用了你的想法,但仍然没有变化,我仍然得到颜色线而不是渐变
  • 你能给出你所拥有的截图,以及你想要的模型吗?这样我(和其他人)就会更好地了解我们的目标:)
  • 我得到了这个link 并且想要类似link 的任何颜色我放入该列表中
猜你喜欢
  • 2010-11-19
  • 2011-01-01
  • 1970-01-01
  • 2011-03-31
  • 2012-01-03
  • 2012-11-28
  • 1970-01-01
  • 2014-10-29
相关资源
最近更新 更多