img-to-webp
is a Python package that provides a command-line interface (CLI) for converting images to the WebP format.
It supports various image formats and allows for resizing images based on predefined rules.
- Convert images from various formats (PNG, JPG, JPEG, GIF, BMP, TIFF) to WebP.
- Resize images using different modes:
cover
,contain
,fill
, ornone
. - Define custom resizing rules based on filename patterns.
- Option to overwrite existing files.
- Verbose logging for detailed processing information.
To install the package, use pip:
pip install img-to-webp
You can use the CLI to process images by specifying the input and output directories, along with other options.
img-to-webp --input-dir INPUT_DIR [options]
--input-dir
: Directory containing the images to be processed.--output-dir
: Directory where the processed images will be saved. If not specified, the output directory will be the same as the input directory.--default-resize-mode
: Mode for resizing images (cover
,contain
,fill
,none
).--default-size
: Default size for resizing images (width, height).--quality
: Quality of the output WebP images (0-100).--overwrite
: Overwrite existing files in the output directory.--verbose
: Enable verbose logging.--config
: Path to a YAML configuration file.
img-to-webp --input-dir ./images \
--output-dir ./webp_images \
--default-resize-mode cover \
--default-size 256 256 \
--quality 80 \
--overwrite \
--verbose
You can also use a YAML configuration file to specify the settings. The CLI will merge the settings from the configuration file with the command-line arguments.
input_dir: ./images
output_dir: ./webp_images
default_resize_mode: cover
resize_rules:
- pattern: ".*_small.*"
size: [ 128, 128 ]
mode: contain
- pattern: ".*_large.*"
size: [ 512, 512 ]
mode: cover
default_size: [ 256, 256 ]
img-to-webp --config config.yaml --overwrite
This project uses uv Python package manager. To set up the development environment, clone the repository and install the dependencies:
git clone https://github.com/nipunchamikara/img-to-webp.git
cd img-to-webp
uv venv
source .venv/bin/activate
uv sync --all-groups
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
For any questions or issues, please open an issue on the GitHub repository.