首页 > 解决方案 > 使用 Python 图像库 (PIL) 保存 gif 图像时动画 GIF 图像像素化

问题描述

我正在尝试在Hug / python 中创建一个图像上传实用程序,并希望保存图像和 gif。但是在上传一些 gif 图像时,gif 图像似乎有很多像素化。下面给出的是我在上传实用程序中使用的代码片段。

 image = Image.open(io.BytesIO(file))
 frames = [frame.copy() for frame in ImageSequence.Iterator(image)]
 image.save(media_location, save_all=True, append_images=frames)

标签: python-3.xfile-uploadpython-imaging-libraryanimated-gifhug

解决方案


解决它!

解决方法可以不使用 PIL 传递动画图像,而是直接使用context manager.

    if image.is_animated:
        with open(media_location, 'wb') as fp:
            fp.write(byte_stream)

推荐阅读