A simple Python script to optimize images by converting them to WebP format, Autometically update them into HTML & CSS and also minifying CSS files.
-
Clone the repository to your local machine:
git clone https://github.com/your-username/webpage_optimizer.git -
Navigate to the project directory:
cd webpage_optimizer -
Install the required dependencies:
pip install -r requirements.txt -
Run the script:
python optimize_images.pyThe script will prompt you to select the directory containing the images to be optimized.
Run the script in your terminal/command prompt. The script will ask you to select the directory containing the images to be optimized. Once the directory is selected, the script will create a new directory with the suffix _output containing the optimized images.
Note: If you're not familier with commandline or python so much then there are two executable files available for this script as well. GUIOptimizer is for Linux users and GUIOptimizer.exe is for Windows users. Make sure to run the correct file for your operating system.
The script optimizes images by:
- Converting images to WebP format: The script converts all
.png,.jpg, and.jpegfiles in the input directory to.webpfiles, which are smaller in size and provide better compression. - Minifying HTML, CSS files: The script minifies all
.htmland.cssfiles in the input directory to reduce their size. - Updating image references in HTML files: The script updates the
srcattribute of all<img>tags in the HTML files to use.webpimages if available. - Adding
altattribute to all images: The script addsaltattribute to all images in the HTML files with the webpage title.
The script uses the following libraries:
tkinterfor creating a graphical user interface (GUI)pyautoguifor selecting the input directoryPillowfor converting images to WebP formatcsscompressorfor minifying CSS filesBeautifulSoupfor parsing HTML files
The script supports the following image formats:
.png.jpg.jpeg
- The script may not work on MacOS if the default web browser is not Google Chrome.
- The script assumes that the input directory has the same structure as the output directory. If the input directory has sub-directories, the script will not create the sub-directories in the output directory.
If you encounter any issues or have suggestions for new features, feel free to create a pull request or open an issue.