from PIL import Image, ImageSequence
# 打开gif图片
image = Image.open('ex.gif')
# 获取所有帧
frames = []
delays = []
for frame in ImageSequence.Iterator(image):
# 获取帧的延迟时间
delays.append(frame.info['duration'])
# 将帧对象转换为Pillow的Image对象
frame_image = frame.convert('RGBA')
# 将帧对象添加到帧列表中
frames.append(frame_image)
# 对所有帧做水平镜像翻转,并保持原有帧的延迟时间不变
mirrored_frames = []
for i, frame in enumerate(frames):
# 水平镜像翻转
mirrored_frame = frame.transpose(method=Image.FLIP_LEFT_RIGHT)
# 将原有延迟时间添加到水平镜像翻转后的帧中
mirrored_frame.info['duration'] = delays[i]
mirrored_frames.append(mirrored_frame)
# 保存水平镜像翻转后的gif图片
mirrored_frames[0].save('example_flipped.gif', save_all=True, append_images=mirrored_frames[1:], format='GIF', loop=0, optimize=True, duration=delays)