Python package wrapping the gifsicle library for editing and optimizing gifs
npx @tessl/cli install tessl/pypi-pygifsicle@1.1.0Python package wrapping the gifsicle library for editing and optimizing GIF images. Provides a simple Python interface to the powerful gifsicle command-line tool, enabling GIF optimization and manipulation from Python scripts.
pip install pygifsiclefrom pygifsicle import gifsicle, optimizefrom pygifsicle import optimize
# Simple optimization - overwrites original file
optimize("my_animation.gif")
# Optimization with destination file
optimize("source.gif", "optimized.gif")
# Using pathlib Path objects
from pathlib import Path
source_path = Path("animations/big_file.gif")
dest_path = Path("animations/small_file.gif")
optimize(source_path, dest_path)Optimizes GIF files using gifsicle with the --optimize flag enabled. This is the most common use case for reducing GIF file sizes while maintaining visual quality.
def optimize(source: Union[str, Path], destination: Optional[str] = None, **kwargs) -> None:
"""
Optimize given gif using gifsicle with optimize flag enabled.
Parameters:
- source: Union[str, Path] - Path to gif image to optimize
- destination: Optional[str] - Output path (defaults to overwrite source)
- **kwargs - Additional arguments passed to gifsicle function (colors, options)
Raises:
- ValueError: If source path doesn't exist or is not a gif file
- FileNotFoundError: If gifsicle binary is not found on system
"""Provides full access to gifsicle functionality with customizable options, colors, and processing parameters. Supports single files, multiple files, and file merging operations.
def gifsicle(
sources: Union[List[str], str, List[Path], Path],
destination: Optional[str] = None,
optimize: bool = False,
colors: int = 256,
options: Optional[List[str]] = None,
) -> None:
"""
Apply gifsicle with given options to image at given paths.
Parameters:
- sources: Union[List[str], str, List[Path], Path] - Path or paths to gif(s) to process
- destination: Optional[str] - Output path (defaults to overwrite first source)
- optimize: bool - Whether to add optimize flag (default: False)
- colors: int - Number of colors to use, must be power of 2 (default: 256)
- options: Optional[List[str]] - Additional gifsicle command-line options
Raises:
- ValueError: If source path doesn't exist, source is not a gif, or destination is not a gif
- FileNotFoundError: If gifsicle binary is not found on system
"""from pygifsicle import optimize
# Optimize in place
optimize("animation.gif")
# Optimize to new file
optimize("large_animation.gif", "small_animation.gif")from pygifsicle import gifsicle
# Merge multiple GIFs into one optimized file
gifsicle(
sources=["part1.gif", "part2.gif", "part3.gif"],
destination="merged.gif",
optimize=True,
colors=128,
options=["--verbose"]
)
# Process with custom color reduction
gifsicle(
sources="input.gif",
destination="output.gif",
colors=64,
options=["--lossy=80"]
)from pygifsicle import optimize, gifsicle
from pathlib import Path
# With Path objects
input_file = Path("animations/source.gif")
output_file = Path("animations/optimized.gif")
optimize(input_file, output_file)
# Multiple Path objects
sources = [Path("gif1.gif"), Path("gif2.gif")]
gifsicle(sources, destination="combined.gif", optimize=True)from typing import Union, List, Optional
from pathlib import Path
# Core types used in the API
str = str # Built-in string type
Path = pathlib.Path # pathlib Path objects for file paths
Union[str, Path] = Union[str, Path] # Either string or Path object
List[str] = List[str] # List of strings
Optional[str] = Optional[str] # Optional string parameterRaised when:
Raised when the gifsicle binary is not found on the system. The error message includes installation instructions:
sudo apt-get install gifsicle (Ubuntu/Debian) or equivalentfrom pygifsicle import optimize
import os
# Optimize all GIFs in a directory
gif_dir = "animations/"
for filename in os.listdir(gif_dir):
if filename.endswith(".gif"):
filepath = os.path.join(gif_dir, filename)
optimize(filepath)from pygifsicle import optimize
try:
optimize("animation.gif")
except FileNotFoundError:
print("gifsicle not installed. Please install gifsicle binary.")
except ValueError as e:
print(f"Invalid file: {e}")