我正在使用 Pillow 库。我有一个 gif,我必须在其上粘贴另一张图像。虽然循环遍历 gif 的每一帧并在其上粘贴图像,但 gif 失真。
请有人为此提出适当的解决方案。
谢谢。
1. 动图
2. 图片
3.代码
from PIL import Image
from io import BytesIO
animated_gif = './img/gif_distort.gif'
transparent_foreground = './img/cat.jpeg'
img = Image.open(animated_gif)
image = Image.open(transparent_foreground)
duration = []
frames = []
for i in range(img.n_frames):
img.seek(i)
frame = img.convert('RGBA').copy()
duration.append(img.info['duration'])
frame.paste(image)
frames.append(frame)
# save gif in temp file
temp_gif_path = 'img/output.gif'
frames[0].save(temp_gif_path, format='GIF', save_all=True, append_images=frames[1:], duration=duration, optimise=True)
结果


