If you want your page to load fast and you want to get some nice score on pagespeed or lighthouse or in general.. you must optimize the size of your images.
I found simple script that I actually optimized even more in order to automate this tedious task for me, might as well share it here, if anybody is interested :)
However, in order to use it, first install following packages
sudo apt-get install optipng advancecomp pngcrush jpegoptim
Then simply use this script in your web source directory, you might configure single directory as an exception, in my case since I use Hugo, it’s themes directory where I do not want to optimize images.
#!/bin/bash
ignore=themes
file=optimg.flag
if [ -f "$file" ]; then
option="-newer $file"
fi
find . -type f $option -not -path "./$ignore/*" -iname "*.png" -exec optipng -nb -nc {} \;
find . -type f $option -not -path "./$ignore/*" -iname "*.png" -exec advpng -z4 {} \;
find . -type f $option -not -path "./$ignore/*" -iname "*.png" -exec pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow {} \;
find . -type f $option -not -path "./$ignore/*" -iregex .*\.jpe?g$ -exec jpegoptim --all-progressive -f --strip-all {} \;
touch $file