This Python script optimizes images by converting them to WebP and resizing them to reduce file size and dimensions. It is particularly useful for optimizing images for web usage, enhancing site performance, and improving SEO by reducing page load times.
- Converts images to the WebP format for better compression and smaller file sizes.
- Resizes images to specified maximum dimensions to ensure optimal display on web pages.
- Iteratively adjusts image quality to meet target file size and dimension constraints.
- Supports processing of various image formats including PNG, JPG, JPEG and WebP.
- Preserves folder structure of input directory in the output directory.
- Clone the repository
- Navigate to the project directory
- Install Pillow with pip:
If 'python3' is not working for you, try with 'python'
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
- Place your images files or folders in the
input
directory. - Run the script:
python3 script.py
- You can check the optimization percentage in real-time in the logs.
- Check the
output
directory for the optimized images.
You can customize the optimization parameters such as:
- Maximum file size (
MAX_SIZE_KB
) - Maximum width (
MAX_WIDTH_PX
) - Default quality (
DEFAULT_QUALITY
)
- Python 3.6 or above
Contributions are welcome! Here are a few ways you can contribute:
- Report bugs or issues
- Suggest new features or enhancements
- Submit pull requests
This project is licensed under the MIT License.