在Python中生成颜色渐变 [英] Generating color gradient in Python
问题描述
我有一个RGB颜色的列表,需要在它们之间绘制渐变在python。
编辑:
我得到这个:
def gradient(list_of_colors):
width = 600
height = 480
img = Image.new )
draw = ImageDraw.Draw(img)
对于范围内的i(len(list_of_colors)):
r1,g1,b1 = list_of_colors [i]
对于x在范围(width / len(list_of_colors)):
color =(r1,g1,b1)
draw.line((x +(width / len(list_of_colors)* i),0,x + (width / len(list_of_colors)* i),height),fill = color)
img.show()
gradient([(30,198,244) (99,200,72),(120,50,80),(200,90,140)])
$ b b
它给我这样:
http://img59.imageshack .us / img59 / 1852 / 3gba.png
我只需要在这些颜色之间渐变而不是条纹。
(类似这样的东西) http://www.kees-tm.nl/ uploads / colorgradient.jpg
我认为这样的代码会工作,它使用 Linear Interpolation 创建渐变。
list_of_colors = [(30,198,244),(99,200,72),(120,50,80),(200,90,140)]
/ pre>
no_steps = 100
def LerpColour(c1,c2,t):
return(c1 [0] +(c2 [0] -c1 [0])* t,c1 [1] +范围(len(list_of_colors)-2)中的i的[1] -c1 [1])* t,c1 [2] +(c2 [2] -c1 [2] ):
对于范围内的j(no_steps):
color = LerpColour(list_of_colors [i],list_of_colors [i + 1],j / no_steps)
显然,我不知道你是如何绘制渐变,所以我把它留给你,你喜欢用颜色变量来绘制每个梯度在for循环中的步骤。 :)
此外:我不明白列表生成,所以如果任何人可以改进LerpColour功能使用它,请编辑我的帖子:)
EDIT -
生成一个列表,在使用PIL绘制时可以很容易地迭代:for in 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))
i have a list of RGB colors and need to draw gradient between them in python. Have you any suggestions how to make it usin PIL library?
EDIT: I get this:
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)])
and it draw me this: http://img59.imageshack.us/img59/1852/3gba.png
I just need to make it gradient between those colors not stripes of colors. (something like this) http://www.kees-tm.nl/uploads/colorgradient.jpg
解决方案I think code like this will work, it uses Linear Interpolation to create the gradient.
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)
Obviously I don't know how you are drawing the gradient so I've left it open to you, do what you like with the colour variable to draw each step of the gradient within the for loop. :)
Also: I don't understand list generation so if anyone can improve the LerpColour function to use it please edit my post :)
EDIT - Generating a list that can easily be iterated over when drawing with 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))
这篇关于在Python中生成颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!