Image Optimization

Table of Contents

Introduction

This document provides step-by-step instructions for optimizing various image formats, including GIF, JPEG, PNG, SVG, and WebP, using different tools. The optimization methods include both lossless and lossy compression techniques, along with parallel execution for improved efficiency.


Warning

Before performing any lossy optimizations, it’s highly recommended to back up your images. Lossy optimizations can permanently reduce image quality, and the changes cannot be undone.


GIF Optimization (Lossy)

Installation:

sudo apt install gifsicle -y

Optimization Command:

find /folder/where/images/are -type f -iname "*.gif" -exec gifsicle --optimize=3 --output {} {} \;

This command optimizes GIF files using the highest available compression level (--optimize=3).


JPEG Optimization

Installation:

sudo apt install jpegoptim -y

Lossless Optimization:

find /folder/where/images/are -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -exec jpegoptim --strip-all --all-progressive -v -p {} \;

This method preserves image quality while removing unnecessary metadata to reduce file size.

Lossy Optimization:

find /folder/where/images/are -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -exec jpegoptim --strip-all --all-progressive -v -p -m80 {} \;

The -m80 flag compresses images with a quality setting of 80, reducing file size at the cost of slight quality loss.

Optimization in Parallel:

find /folder/where/images/are -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) | xargs -P 2 -I {} jpegoptim --strip-all --all-progressive -v -p -m90 {}

This command optimizes images using two parallel processes, improving efficiency for large image batches.


Guetzli JPEG Optimization (Lossy, High Quality)

Installation:

sudo apt install guetzli -y

Optimization Command:

find /folder/where/images/are -type f -iname "*.jpg" -exec guetzli --quality 84 {} {} \;

Guetzli provides high-quality lossy JPEG compression optimized for web use.

Note: Guetzli uses a large amount of memory. You should provide 300MB of memory per 1MPix of the input image.


Installing Cargo (Prerequisite for Rust-Based Optimization Tools)

curl https://sh.rustup.rs -sSf | sh
source "$HOME/.cargo/env"

This installs Cargo, the Rust package manager, required for tools like Oxipng and Pngquant.


PNG Optimization

Lossless PNG Optimization with Oxipng

Installation:
git clone https://github.com/shssoichiro/oxipng.git
cd oxipng
cargo build --release -j`nproc`
cp target/release/oxipng /usr/bin
Optimization Command:
find /folder/where/images/are -type f -iname "*.png" -exec oxipng -o 3 -s -p -t 2 -a {} \;

The -a option improves compression for images with an alpha channel while maintaining visual quality.


Lossy PNG Optimization with Pngquant

Installation:
git clone --recursive https://github.com/kornelski/pngquant.git
cd pngquant
cargo build --release -j`nproc`
cp target/release/pngquant /usr/bin
Optimization Command:
find /folder/where/images/are -type f -iname "*.png" -exec pngquant -v --quality=90-95 --skip-if-larger --ext .png --strip --force {} \;
Optimization in Parallel:
find /folder/where/images/are -type f -iname "*.png" -print0 | xargs -0 -P 2 -I {} pngquant -v --quality=90-95 --skip-if-larger --ext .png --strip --force "{}"

Note: Be careful running optimization commands as root. Optimized files may be owned by root, requiring a chown command to restore ownership.


WebP Conversion

Installation:

curl -o /usr/bin/magick https://imagemagick.org/archive/binaries/magick
chmod a+x /usr/bin/magick

Optimization Command:

find /folder/where/images/are -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) -exec magick {} -quality 85 {}.webp \;

This converts images to WebP format with a quality setting of 85.


SVG Optimization

Installation:

Install Node.js and npm using this link.

Optimization Command:

npm install -g svgo
find /folder/where/images/are -type f -iname "*.svg" -exec svgo --multipass --pretty {} \;

This optimizes SVG files by removing unnecessary data while preserving visual integrity.


comments powered by Disqus