Skip to content

Instantly share code, notes, and snippets.

@Teqqles
Created May 10, 2018 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Teqqles/abe34e581bf3d2387e03a8e479face5f to your computer and use it in GitHub Desktop.
Save Teqqles/abe34e581bf3d2387e03a8e479face5f to your computer and use it in GitHub Desktop.
Adding a diagonal gradient to a PIL image - implemented in a hack day
from PIL import Image
import math
def gradient_fill(im, from_color, to_color):
start_x = 0
start_y = 0
pixel_data = im.load()
for x in range(im.size[0]):
for y in range(im.size[1]):
dist = math.fabs(start_x - x) + math.fabs(start_y - y)
start_rgb = (184, 108, 0)
end_rgb = (255, 180, 0)
# reduce distance travelled to a percentage of the total image size
dist = dist / (im.size[0] + im.size[1])
# run through the rgb object and calculate relative distances between start+end
r, g, b = map(lambda start, end: start+end,
map(lambda start: start*(1-dist), from_color),
map(lambda end: end*dist, to_color))
pixel_data[x, y] = int(r), int(g), int(b)
return im
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment