python画图渐变颜色的代号,在Python中生成颜色渐变

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值