I'm having trouble figuring out how to fade an image that has transparency from the top, I know something like this works, however, manipulating it to fade from the top instead of the bottom is proving quite difficult. I was wondering if I could get some help.
Current code for fading images (towards the bottom, not top, I need top and bottom) looks like:
def image_fade(im: Image, fade_start, fade_finish, from_top: bool = False):
width, height = im.size
pixels = im.load()
fade_range = list(range(int(height*fade_start), int(height*fade_finish)))
if from_top:
for y in fade_range:
for x in range(width):
alpha = int((y - height*fade_start)/height/(fade_finish - fade_start) * 255)
alpha = 0 if alpha <= 0 else alpha
pixels[x, y] = pixels[x, y][:3] + (alpha,)
for y in range(height, y):
for x in range(width):
pixels[x, y] = pixels[x, y][:3] + (0,)
else:
for y in fade_range:
for x in range(width):
alpha = pixels[x, y][3]-int((y - height*fade_start)/height/(fade_finish - fade_start) * 255)
alpha = 0 if alpha <= 0 else alpha
pixels[x, y] = pixels[x, y][:3] + (alpha,)
for y in range(y, height):
for x in range(width):
pixels[x, y] = pixels[x, y][:3] + (0,)
return im
Fade start and finish should be equal on both sides, meaning if we pass the parameters 0.7, 0.9 for fade_start and fade_finish, the top half should be (1-fade_finish), (1-fade_start), or (0.1, 0.3)
From something like:
I am trying to get something like (where the background is transparent, not white, say I want 10% of the top and bottom faded):





