Skip to content

Instantly share code, notes, and snippets.

@JupyterJones
Created February 15, 2023 03:54
Show Gist options
  • Save JupyterJones/c2bd941b6ad2b14af2f06c934ec07690 to your computer and use it in GitHub Desktop.
Save JupyterJones/c2bd941b6ad2b14af2f06c934ec07690 to your computer and use it in GitHub Desktop.
Use Pil to create a gif - This opens a forground image and paste it small and with transparency then slowly increases opacity and size
from PIL import Image, ImageSequence
def zoom_effect(bg_file, fg_file):
bg = Image.open(bg_file).convert('RGBA')
bg = bg.resize((640,640), Image.BICUBIC)
fg = Image.open(fg_file).convert('RGBA')
fg = fg.resize((bg.size), Image.BICUBIC)
fg_copy = fg.copy()
fg_copy = fg_copy.resize((int(fg_copy.width), int(fg_copy.height)))
result_images = []
for i in range(10):
size = (int(fg_copy.width * (i+1)/10), int(fg_copy.height * (i+1)/10))
fg_copy_resized = fg_copy.resize(size)
fg_copy_resized.putalpha(int((i+1)*255/10))
fg_copy_resized = fg_copy_resized.convert('RGBA')
fg_copy_resized.putalpha(int((i+1)*255/10))
result = bg.copy()
x = int((bg.width - fg_copy_resized.width)/2)
y = int((bg.height - fg_copy_resized.height)/2)
result.alpha_composite(fg_copy_resized, (x, y))
result.save("gifs/_"+str(i)+".png")
result_images.append(result)
# Save the resulting images as a GIF animation
result_images[0].save('gifs/zoom_effect.gif', save_all=True, append_images=result_images[1:], optimize=False, duration=100, loop=0)
#zoom_effect(bg_file, fg_file)
zoom_effect("gifs/background.jpg", "gifs/focus.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment