Imageio in python: compressing gif
Is there a way to compress the gif when created imageio
in python
? I am making a gif with about 200 images and the final file is 30MB. I would prefer if it is 5-10 MB. In any case, mono color images should be well compressed. Is there a tool I can use or point to with imageio
?
Here is my code to make a gif:
import os
import imageio as io
import re
#############################################################
#key to sort the file_names in order
numbers = re.compile(r'(\d+)')
def numericalSort(value):
parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
############################################################
file_names = sorted((fn for fn in os.listdir('.') if fn.startswith('surface')), key = numericalSort)
#gif writer
with io.get_writer('python_growth.gif', mode='I', duration=0.1) as writer:
for filename in file_names:
image = io.imread(filename)
writer.append_data(image)
source to share
Faced with the same problem, I created a wrapper for a gifsicle library named pygifsicle and it can be used like this:
from pygifsicle import optimize
optimize("path_to_my_gif.gif")
Like any other package in pip, it can be installed by running:
pip install pygifsicle
A complete example of using this library is available in the imageio documentation .
When installing pygifsicle, you will also automatically install, if you are using macOS, the gifsicle library using Brew . For other systems, a step-by-step tutorial will be provided which basically just asks to install the library using apt-get on Debian / Ubuntu (since it seems like a good idea not to ask for sudo in the package setup):
sudo apt-get install gifsicle
Or on Windows, you can set one of the available ports .
source to share