mozjpeg
allows optimizing the compression, losslessly recompressing an image.
mozjpeg input.jpg > output.jpg
Depending on how it's installed, the executable might be called jpegtran
instead of mozjpeg
. To convert other formats to jpg, use cjpeg
, or the convert
command from imagemagick, followed by mozjpeg
.
There is also guetzli
, but I never got good results with it.
The main minifier is svgo.
If the SVG image paths are breaking, turn off convertPathData
and mergePaths
.
If colors are turning black or being removed, turn off removeUnknownsAndDefaults
.
There have also been reports of issues with collapseGroups
, though I've never had an issue yet.
Usage example:
svgo --multipass --disable=removeUnknownsAndDefaults input.name -o output.name
Achtung: In some weird cases, --multipass
actually compresses less than if not used.
For best results, use pngquant, followed by zopflipng.
pngquant --speed 1 input.png
By default, the new image will be called input-fs8.png
. If the image is very simple color-wise, we can try to really squeeze it using a low quality range, e.g.:
pngquant --speed 1 --quality=0-10 input.png
For the best results (although for most images it takes hours):
zopflipng --iterations=1000 --filters=01234mepb --lossy_8bit --lossy_transparent
gifsicle -O3 input.gif -o output.gif
#!/bin/bash
extension="${1##*.}"
backup=${1%${extension}}orig.${extension}
mv "$1" "${backup}"
optimize_svg() {
# For some reason, sometimes not using --multipass compresses better
# Since svgo is fast, we just try both and keep whichever is smaller.
svgo --multipass --disable=removeUnknownsAndDefaults "${backup}" -o temp1.svg
svgo --disable=removeUnknownsAndDefaults "${backup}" -o temp2.svg
size1=$(stat -c %s temp1.svg)
size2=$(stat -c %s temp2.svg)
if [[ "$size1" -lt "$size2" ]]; then
mv temp1.svg "$1"
rm temp2.svg
else
mv temp2.svg "$1"
rm temp1.svg
fi
}
case ${extension} in
"png")
zopflipng --iterations=1000 --filters=01234mepb --lossy_8bit --lossy_transparent "${backup}" "$1"
;;
"gif")
gifsicle -O3 "${backup}" -o "$1"
;;
"jpeg")
;&
"jpg")
mozjpeg "${backup}" > "$1"
;;
"svg")
optimize_svg "$@"
;;
*)
echo "Format .${extension} unsupported."
exit 1
;;
esac