How to Compress & Optimize JPEGs, PNGs, and GIFs Using Command Line Tools?

Optimizing images is crucial for enhancing website performance and ensuring a better user experience. By using command-line tools like jpegoptim, OptiPNG, and Gifsicle, you can efficiently reduce the file sizes of JPEGs, PNGs, and GIFs without compromising their quality. This detailed guide will walk you through the process of installing these tools, using them to optimize your images, and even automating the optimization process with scripts. Whether you’re a web developer, designer, or just looking to improve your image handling, this guide will provide you with the necessary steps and explanations to get the best results from your image optimization efforts.

In this detailed guide How to compress and optimize JPEGs, PNGs, and GIFs using command line tools, we will explore three widely-used command-line tools – jpegoptim, OptiPNG, and Gifsicle to optimize JPEGs, PNGs, and GIFs efficiently. Each tool has unique features, and by the end of this guide, you will be able to optimize images for a range of use cases.

#I Optimizing JPEGs with jpegoptim

JPEG is one of the most common image formats used on the web due to its high compression ratio and reasonable quality retention. However, unoptimized JPEG images can still result in unnecessarily large file sizes. jpegoptim is a specialized tool for optimizing JPEG images by adjusting compression settings and removing metadata (such as EXIF and comment data) that may not be necessary.

Unlike many image editors, jpegoptim offers granular control over the quality and size of images from the command line. You can perform both lossless and lossy optimizations.

#II How JPEG Compression Works

JPEG compression uses a technique called DCT (Discrete Cosine Transform) to remove redundant image data, which allows the file size to shrink. Lossless optimization with jpegoptim focuses on reducing file size without affecting image quality by optimizing the existing compression structure. Lossy compression, on the other hand, can achieve more significant size reduction by slightly degrading image quality. When done properly, this degradation is often imperceptible to the human eye.

Installation

On Ubuntu/Debian

sudo apt install jpegoptim

On macOS (via Homebrew)

brew install jpegoptim

On Windows — You can install jpegoptim using the Windows Subsystem for Linux (WSL) or package managers like Chocolatey:

choco install jpegoptim

Basic Usage

Lossless Optimization — This mode retains the original quality of the image but reduces the file size by optimizing the JPEG’s internal structure.

jpegoptim your-image.jpg

The default operation strips unnecessary data and optimizes the Huffman tables to reduce file size without altering the image quality.

Lossy Compression — If you are willing to compromise on image quality for a smaller file size, you can specify a maximum quality level (typically between 70 and 85 for web use).

jpegoptim --max=80 your-image.jpg

This will reduce the quality to 80%, striking a balance between file size reduction and image clarity.

Removing EXIF Data — JPEG images often carry metadata (EXIF) containing information about the camera, settings, and location where the image was taken. While useful for photographers, this metadata is often unnecessary for web use. Removing it can significantly reduce the file size.

jpegoptim --strip-all your-image.jpg

Batch Processing Multiple JPEGs — Optimizing multiple images at once can save you time and effort. You can pass a list of images or a wildcard pattern.

jpegoptim .jpg

Example Command
To optimize a JPEG image with a maximum quality setting of 75% and remove all metadata:

jpegoptim --max=75 --strip-all your-image.jpg

Key Options

  • –max=N – Sets the maximum image quality (0-100) for lossy optimization
  • –strip-all – Removes all metadata, including EXIF and comments
  • -d DIR – Specifies a target directory to save the optimized file
  • -S SIZE – Sets a target file size for the output

#III Optimizing PNGs with OptiPNG

PNG is widely used for images that require lossless compression, such as logos, icons, and illustrations. The PNG format uses DEFLATE compression, which is lossless by nature. However, PNG files can still become bloated due to metadata, inefficient encoding, or excessive color depth. OptiPNG is a command-line utility designed to reduce PNG file sizes by optimizing the compression parameters and stripping unnecessary metadata, all while preserving the original image quality.

Why Optimize PNGs

Since PNG files are lossless, their file size is often larger than JPEGs, especially for images with a lot of detail. Optimizing them helps keep websites lightweight without sacrificing transparency or quality.

Installation

On Ubuntu/Debian

sudo apt install optipng

On macOS

brew install optipng

Basic Usage

Basic Optimization – Running the tool with no options applies basic lossless compression to reduce file size.

optipng your-image.png

Adjusting Optimization Levels: You can choose the level of optimization with the -o flag. The levels range from -o0 (fastest, least compression) to -o7 (slowest, most compression).

optipng -o7 your-image.png

While -o7 yields the smallest file size, it may take more time to process, especially for large images.

Stripping Metadata – Metadata such as color profiles and comments can inflate the file size unnecessarily. Stripping them can further reduce file size without any visible changes to the image.

optipng -strip all your-image.png

Batch Processing: Optimize multiple PNG files in a directory with a single command.

optipng .png

Example Command
To optimize a PNG image with maximum compression and remove all metadata:

optipng -o7 -strip all your-image.png

Key Options

  • oN – Sets the optimization level (0-7)
  • strip all – Removes all unnecessary metadata chunks (e.g., text comments, color profiles).
  • dir DIR – Saves the optimized file in a specified directory.
  • backup – Keeps a backup of the original file.

#IV Optimizing GIFs with Gifsicle

Animated GIFs are notorious for being heavy due to the frame-by-frame nature of the format. Optimizing them can reduce the file size by compressing frames, removing redundant data, and adjusting the color palette. This is especially important for websites or social media platforms where GIFs are used frequently.

On Ubuntu/Debian

sudo apt install gifsicle

On macOS

brew install gifsicle

Basic Usage

Basic Optimization — Use gifsicle to perform a simple optimization. The O3 option applies the maximum level of optimization.

gifsicle -O3 your-image.gif -o optimized-image.gif

Resizing GIFs — Resizing a GIF is one of the most effective ways to reduce file size, as smaller dimensions require fewer pixels to animate.

gifsicle --resize 320x240 your-image.gif -o resized-image.gif

Reduce Number of Colors: By reducing the number of colors in a GIF, you can shrink the file size while still maintaining acceptable visual quality.

gifsicle --optimize=3 --colors 64 your-animated.gif -o optimized-image.gif

Remove Metadata — Stripping metadata from GIFs can also contribute to size reduction.

gifsicle --no-comments --no-extensions your-image.gif -o optimized-image.gif

Example Command

To optimize a GIF with maximum compression, resize it, and reduce the number of colors:

gifsicle -O3 –resize 320×240 –colors 64 your-image.gif -o optimized-image.gif

  • ON – Specifies the optimization level (0-3)
  • –resize WIDTHxHEIGHT – Resizes the GIF to new dimensions.
  • –optimize=N – Optimizes the GIF (higher numbers indicate more optimization).
  • –colors N – Reduces the number of colors to N.
  • –no-comments – Removes comments from the GIF to save space.

#V Batch Processing Multiple Images

You can automate the optimization of multiple images by using a loop in the terminal. Below are examples for batch processing JPEGs, PNGs, and GIFs:

Batch Process JPEGs

for img in .jpg; do jpegoptim --max=80 --strip-all "$img"; done

Batch Process PNGs

for img in .png; do optipng -o7 "$img"; done

Batch Process GIFs

for gif in .gif; do gifsicle -O3 "$gif" -o "optimized-$gif"; done

Best Practices for Image Optimization

  • Choose the Right Format — Use JPEG for photographs, PNG for images with transparency or sharp details (like logos), and GIF for simple animations.
  • Balance Quality and Size — For lossy formats like JPEG, test different quality settings to find a balance between file size and acceptable visual quality
  • Automate the Process — If you work with many images, create a script that automatically optimizes images as part of your workflow.
  • Use Multiple Tools — Sometimes, combining tools (e.g., using jpegoptim followed by another compressor) can yield even smaller file sizes.

By using jpegoptim, OptiPNG, and Gifsicle, you can significantly reduce image sizes for JPEGs, PNGs, and GIFs, which is critical for optimizing website performance and reducing bandwidth usage. These tools are lightweight, efficient, and perfect for developers, web designers, and anyone looking to optimize images directly from the command line. Whether you are optimizing a few images or handling thousands, these tools can save you both time and storage space.