How do you scale an animated GIF in PIL and save the animation

I am wondering if it is possible to scale an animated GIF using PIL. In particular, the Plones ImageField archetypes are currently losing animation from images scaled using the zoom method:

def scale(self, data, w, h, default_format = 'PNG'): """ scale image (with material from ImageTag_Hotfix)""" #make sure we have valid int's size = int(w), int(h) original_file=StringIO(data) image = PIL.Image.open(original_file) # consider image mode when scaling # source images can be mode '1','L,','P','RGB(A)' # convert to greyscale or RGBA before scaling # preserve palletted mode (but not pallette) # for palletted-only image formats, eg GIF # PNG compression is OK for RGBA thumbnails original_mode = image.mode img_format = image.format and image.format or default_format if original_mode == '1': image = image.convert('L') elif original_mode == 'P': image = image.convert('RGBA') image.thumbnail(size, self.pil_resize_algo) # decided to only preserve palletted mode # for GIF, could also use image.format in ('GIF','PNG') if original_mode == 'P' and img_format == 'GIF': image = image.convert('P') thumbnail_file = StringIO() # quality parameter doesn't affect lossless formats image.save(thumbnail_file, img_format, quality=self.pil_quality) thumbnail_file.seek(0) return thumbnail_file, img_format.lower() 

I know how to identify an animated GIF: the following evaluates to True image.format == 'GIF' and image.seek(image.tell()+1) . Ive tried not to switch to RGBA mode, but this does not work.

Reference Information. In our Plone instance, we changed the default image type to set the original_size attribute of our image field so that all images are scaled with the appropriate quality setting. This works fine for jpegs, but means that we are currently unable to load animated GIFs.

+6
source share
3 answers

PIL has limited support for animated GIFs, but it is said to be limited, and you need to work at a very low level to deal with it.

I would suggest trying a different image scaling method than PIL if you want to deal with animated gifs. Perhaps the easiest way is to use an out-of-process ImageMagick with the .Popen subprocess - (and even then, at this time, I just assume that ImageMagick is "doing the right thing" with animated GIFs) -

The option is to have an "image processing server" with a different Python script, besides your zope installation, which will receive requests for image scaling - perhaps by calling xmlrpc - you can build it like a GIMP-plug -in and use GIMP to scale GIF.

Another option is to leave things as they are and use โ€œstill imagesโ€ for the animated GIF, where they should appear in a different dimension than the original, and display the original image in which the animation fits. (Or just ask the animated gif to be presented in the correct size)

+4
source

You can use images2gif.py to read gifs and scale each frame independently. images2gif lets you write an animated gif with a sequence of images.

image2gif.py, which I found on the Internet, did not handle transparency, so I fixed it. You can find it here: https://bitbucket.org/bench/images2gif.py

+7
source

Pillow (PIL fork) supports animated GIFs from version 3.0.0 when PR # 1384 was merged.

0
source

Source: https://habr.com/ru/post/907565/


All Articles