我目前正在尝试将一个gif放在gif的顶部,然后保存它。我试过了。
@client.command()
async def salt(ctx, member: discord.Member):
response = requests.get(member.avatar_url)
img = Image.open(BytesIO(response.content))
framess = []
framess.append(img)
framess[0].save('png_to_gif.gif', format='GIF',
append_images=framess[1:],
save_all=True,
duration=100, loop=0)
new_Igf = Image.open('png_to_gif.gif')
animated_gif = Image.open("salty.gif")
frames = []
for frame in ImageSequence.Iterator(new_Igf):
frame = frame.copy()
frame.paste(animated_gif)
frames.append(frame)
frames[0].save("iamge.gif")这样做的目的是从url中获取图像,并将其转换为gif格式。在本地打开gif并尝试将其应用于转换后的gif。
与我期望的不同,我得到了一个奇怪的非动画文件。
图像link> https://cdn.discordapp.com/attachments/738572311107469354/785428570205716491/iamge.gif
请帮帮忙。使用discord.py和Pillow。
发布于 2020-12-14 07:55:23
from PIL import Image, ImageSequence
# load image
background = Image.open('lenna.png')#.convert('RGBA')
animated_gif = Image.open("salty.gif")
all_frames = []
for gif_frame in ImageSequence.Iterator(animated_gif):
# duplicate background image because we will change it
new_frame = background.copy()
# need to convert from `P` to `RGBA` to use it in `paste()` as mask for transparency
gif_frame = gif_frame.convert('RGBA')
# paste on background using mask to get transparency
new_frame.paste(gif_frame, mask=gif_frame)
all_frames.append(new_frame)
# save all frames as animated gif
all_frames[0].save("image.gif", save_all=True, append_images=all_frames[1:], duration=50, loop=0)lenna.png (维基百科:Lenna)

salty.gif

image.gif

https://stackoverflow.com/questions/65185415
复制相似问题