Imageio in python : compressing gif Imageio in python : compressing gif python python

Imageio in python : compressing gif


Faced the very same problem, I've created a wrapper for gifsicle library called pygifsicle and one can use it as follows:

from pygifsicle import optimizeoptimize("path_to_my_gif.gif")

As every other package on pip, one can install it by running:

pip install pygifsicle

A full example for using this library is available in the imageio documentation.

While installing pygifsicle you will automatically install also, if you are on MacOS, the gifsicle library using Brew. For the other systems, a step-by-step guide will be provided, which it basically just asks to install the library via apt-get on Debian / Ubuntu (since it seems a good idea to not ask for sudo within the package setup):

sudo apt-get install gifsicle

Or on windows you can install one of the available ports.


Another method is to resize and reduce the quality of an image before you create the gif.

from PIL import Image# Resizingimage.resize((x, y), Image.ANTIALIAS)# Reducing Qualityquality_val = 90image.save(filename, 'JPEG', quality=quality_val)

Full code for turning images into compressed gif

from PIL import Imageimport globx = 250y = 250fp_in = 'path/to/images'fp_in = 'path/to/gif/output'q = 50 # Qualityimg, *imgs = [Image.open(f).resize((x,y),Image.ANTIALIAS) for f in sorted(glob.glob(fp_in))] img.save(fp=fp_out, format='GIF', append_images=imgs,quality=q,          save_all=True, duration=15, loop=0, optimize=True)