[Python] 2 lines of code to achieve gif compression (pygifsicle)

Write in front

I have shared with you how to pythoncombine multiple pictures into a gif animation (click here to view). But what if the synthesized animation is too big, how to compress it?

Dependent modules

pip install pygifsicle

Sample code

from pygifsicle import optimize
optimize('test.gif', "optimized.gif") # For creating a new one

Parameter Description

test.gif: This is the gifname of the file to be compressed
optimized.gif: this is the name of the compressed giffile

Precautions

Compression requires gifsicle.exeprogram support, and different systems have different solutions.

MacOS

When the pip install pygifsiclecommand is executed , the Brewinstallation will be used automatically gifsicle, so no action is required.

Linux

Please use the sudo apt-get install gifsiclecommand to install gifsicle.

Windows

Download the gifsicleprogram yourself, click here to download. After downloading, gifsicle.exeput .pyit under the same level file.

Test Results

Before compression: 44.5KB After compression: 6.47KB

Tips

No, pythonit doesn't matter, we have directly packaged a GIF compression software here, you can use it directly, click here to download.

Citation reference

https://imageio.readthedocs.io/en/stable/examples.html

Guess you like

Origin blog.csdn.net/qq_42951560/article/details/115058307