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.py
The 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.jpeg
files in the input directory to.webp
files, which are smaller in size and provide better compression. - Minifying HTML, CSS files: The script minifies all
.html
and.css
files in the input directory to reduce their size. - Updating image references in HTML files: The script updates the
src
attribute of all<img>
tags in the HTML files to use.webp
images if available. - Adding
alt
attribute to all images: The script addsalt
attribute to all images in the HTML files with the webpage title.
The script uses the following libraries:
tkinter
for creating a graphical user interface (GUI)pyautogui
for selecting the input directoryPillow
for converting images to WebP formatcsscompressor
for minifying CSS filesBeautifulSoup
for 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.