Last active
May 8, 2024 09:09
-
-
Save weihanglo/1e754ec47fdd683a42fdf6a272904535 to your computer and use it in GitHub Desktop.
Draw gradient color with Pillow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome nice bunny!