You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python PIL实现从左到右的水平双色线性渐变?

水平双色渐变实现调整方案

你现有代码生成对角渐变的原因是绘制的线段为对角方向,只需调整绘制逻辑为按列绘制同色竖线,即可实现从左到右的水平渐变效果。

核心修改点

  • 插值区间从gradient.width * 2改为gradient.width,匹配水平方向的总像素列数
  • 将绘制线段的坐标参数[(i, 0), (0, i)]修改为[(i, 0), (i, gradient.height)],代表绘制第i列从顶部到底部的完整竖线

修改后完整代码

from PIL import Image, ImageDraw

def interpolate(f_co, t_co, interval):
    det_co =[(t - f) / interval for f , t in zip(f_co, t_co)]
    for i in range(interval):
        yield [round(f + det * i) for f, det in zip(f_co, det_co)]


imgsize=(1920,1080)
gradient = Image.new('RGBA', imgsize, color=0)
draw = ImageDraw.Draw(gradient)

f_co = (253, 46, 216)
t_co = (23, 214, 255)
# 调整插值区间为画布宽度,按列绘制竖线
for i, color in enumerate(interpolate(f_co, t_co, gradient.width)):
    draw.line([(i, 0), (i, gradient.height)], tuple(color), width=1)
gradient.show()

内容的提问来源于stack exchange,提问作者Jose Manuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:27:01