A Python utility that automatically converts image files to WebP format throughout a directory and all its subdirectories.
This script recursively searches through the current directory and all subdirectories for image files (JPEG, PNG, TIFF, BMP) and GIF files, then converts them to WebP format. The original files are preserved, and WebP versions are created alongside them.
WebP is a modern image format that provides superior compression for images on the web, resulting in smaller file sizes while maintaining visual quality.
- Automatically detects and installs the required dependency (
cwebp
) if missing - Supports recursive conversion (including all subdirectories)
- Preserves original files
- Supports multiple image formats:
- JPEG (.jpg, .jpeg)
- PNG (.png)
- TIFF (.tiff, .tif)
- BMP (.bmp)
- GIF (.gif)
- Provides progress tracking with clear visual feedback
- Configurable conversion parameters
- Python 3.6 or higher
- cwebp (automatically installed if missing on supported platforms)
- Download the
webp-converter.py
script - Make the script executable (optional, for Unix-like systems):
chmod +x webp-converter.py
- Place the script in the root directory where your images are located
- Run the script:
python3 webp-converter.py
That's it! The script will:
- Check if the required
cwebp
dependency is installed - Install it if needed (on supported platforms)
- Find all supported image files in the current directory and subdirectories
- Convert each file to WebP format with default settings
You can customize the conversion parameters by passing them as command-line arguments:
python3 webp-converter.py -q 80 -m 5
-q <quality>
: Quality factor (0-100, default: 70)-m <compression>
: Compression method (0-6, default: 6, higher=slower)-lossless
: Use lossless compression-exact
: Preserve RGB values in transparent areas-z
: Use Zlib compression-mt
: Use multi-threading for encoding
For a complete list of parameters, see the cwebp documentation.
The script:
- Checks for and installs the required
cwebp
dependency if needed - Recursively finds all supported image files
- For each file, creates a WebP version with the same base name and a
.webp
extension - Shows progress during conversion
- Linux: Supports apt, dnf, yum, pacman, and zypper package managers
- macOS: Supports installation via Homebrew
- Windows: Automatic installation not supported, but the script will work if
cwebp
is manually installed and added to PATH
- Permission denied: Run with
sudo
or ensure you have write permissions to the directories - cwebp installation fails: Install manually following the official instructions
- File not found errors: Ensure file paths don't contain special characters
This project is open-source and free to use. Feel free to modify and distribute as needed.