简体   繁体   中英

Animated GIF images getting pixelated when saving gif image with Python Image Library(PIL)

I am trying to create an image upload utility in Hug / python and wanted to save images and gifs. But on uploading some of the gif images, the gif images seem to be a lot pixelated. Given below is the code snippet that I am using in the upload utility.

 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)
  • The original GIF :

    via GIPHY


    在此处输入图像描述


    The uploaded GIF

    在此处输入图像描述

Fixed it!

The workaround can be not passing the animated images with PIL and directly storing it with a context manager .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM