This Python script automates the process of converting JPG, JPEG, and PNG images to the WebP format. It's designed to process images in bulk, making it ideal for optimizing large image collections for web use.
- The optimal application scenario involves converting images generated by Stable Diffusion into the WebP format, primarily to optimize storage efficiency. Additionally, this process includes embedding relevant metadata into the converted images.
- Converts JPG, JPEG, and PNG images to WebP format
- Maintains image quality with configurable compression settings
- Preserves PNG metadata in the converted WebP files
- Supports recursive directory scanning
- Provides detailed logging for easy troubleshooting
- Utilizes multi-threading for improved performance
- Includes a batch script for easy execution from anywhere on Windows systems
- Python 3.6 or higher
- Pillow library
- WebP command-line tools (
cwebp
) - ExifTool
-
Clone this repository or download the script files:
git clone https://github.com/RenX86/WebP-Image-Converter.git
or download
WebP-Image-Converter.py
andrun-webp-converter.bat
directly. -
Install the required Python libraries:
pip install pillow
-
Install WebP command-line tools:
- For Windows: Download from Google's WebP page and add to your PATH.
- For macOS: Use Homebrew:
brew install webp
- For Linux: Use your distribution's package manager, e.g.,
sudo apt-get install webp
-
Install ExifTool:
- For Windows: Download from ExifTool website and add to your PATH.
- In windows rename the 'exiftool(-k).exe' to 'exiftool.exe' after extraction.
- Add the whole extrated folder to PATH in environment variables.
- For macOS: Use Homebrew:
brew install exiftool
- For Linux: Use your distribution's package manager, e.g.,
sudo apt-get install libimage-exiftool-perl
- For Windows: Download from ExifTool website and add to your PATH.
-
(Optional for Windows users) Add the script directory to your system PATH to run it from anywhere:
- Right-click on "This PC" or "My Computer" and select "Properties"
- Click on "Advanced system settings"
- Click on "Environment Variables"
- Under "System variables", find and select the "Path" variable, then click "Edit"
- Click "New" and add the full path to the directory containing your scripts
- Click "OK" to close all dialogs
- Open a command prompt.
- Navigate to the script directory or run from anywhere if you've added it to your PATH.
- Run the batch file:
run-webp-converter
- Enter the path to the folder containing your images when prompted.
- No need enter the path in " " format justy directly paste the path and proceed.
- Open a terminal.
- Navigate to the script directory.
- Run the Python script directly:
python WebP-Image-Converter.py
- Enter the path to the folder containing your images when prompted.
You can modify the following variables at the top of WebP-Image-Converter.py
to customize the script's behavior:
WEBP_QUALITY
: Set the quality of the WebP conversion (0-100)DELETE_ORIGINALS
: Set toTrue
to delete original files after conversion,False
to keep themMAX_WORKERS
: Set the maximum number of concurrent threads for processing
The script provides detailed logging information. Check the console output for progress updates and any error messages.
Contributions to improve the script are welcome. Please feel free to submit a Pull Request.
- This script uses the WebP tools developed by Google
- ExifTool by Phil Harvey is used for metadata handling
- Original Script repo - https://github.com/TCNOco/TcNo-Random-Scripts.git