Skip to content

Instantly share code, notes, and snippets.

@weihanglo
Last active June 15, 2023 04:10
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save weihanglo/1e754ec47fdd683a42fdf6a272904535 to your computer and use it in GitHub Desktop.
Save weihanglo/1e754ec47fdd683a42fdf6a272904535 to your computer and use it in GitHub Desktop.
Draw gradient color with Pillow
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw
im = Image.open('img_original.png')
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)]
gradient = Image.new('RGBA', im.size, color=0)
draw = ImageDraw.Draw(gradient)
f_co = (13, 255, 154)
t_co = (4, 128, 30)
for i, color in enumerate(interpolate(f_co, t_co, im.width * 2)):
draw.line([(i, 0), (0, i)], tuple(color), width=1)
im_composite = Image.alpha_composite(gradient, im)
im_composite.show()
with open('img_result.png', 'wb') as f:
im_composite.save(f)
@osfunapps
Copy link

Awesome nice bunny!

@krivalex
Copy link

cool, bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment