To compress PNGs on Linux from the command line, use standard open-source tools that handle both lossy and lossless reduction. The main search phrase compress png linux points to two primary approaches: pngquant for reducing color depth (lossy) and OptiPNG or oxipng for lossless file size reduction. Install them, learn the common commands, and run batch jobs to shrink PNG images directly in the terminal.

Installing the Tools

On Debian and Ubuntu, open a terminal and run this command to install the two most common PNG tools:

sudo apt install pngquant optipng

This gives you the lossy converter and the lossless optimizer. For oxipng, a multithreaded lossless optimizer, install it separately via your distribution's package manager or build from source. After installation, verify each tool is available by typing its name in the terminal.

Lossy Compression with Pngquant

pngquant is a lossy compressor that converts images to 256 or fewer colors while preserving alpha transparency. Use the pngquant command with the --quality option to set a target quality range. For example:

pngquant --quality=65-80 image.png

By default, pngquant writes a new file with a -fs8.png suffix (e.g., image-fs8.png). To overwrite the original file instead, add the --ext .png --force options:

pngquant --quality=65-80 --ext .png --force image.png

This replaces the input file with the compressed version. Adjust the quality values (here 65 to 80) to balance file size and visual quality.

Lossless Optimization with OptiPNG

OptiPNG reduces file size without changing pixel data. Its optimization levels run from -o0 (fast, minimal) to -o7 (slow, most thorough). Higher levels try more combinations and take longer. A practical choice is:

optipng -o2 image.png

Batch Processing With OptiPNG

For batch processing, use the find command to run OptiPNG on many files at once. A typical optipng example for a directory tree:

find . -name '*.png' -exec optipng -o2 {} +

This applies level 2 optimization to every PNG file in the current directory and subdirectories.

Lossless Optimization with Oxipng

oxipng is a multithreaded lossless optimizer that processes PNGs faster on multi-core systems. Its optimization levels range from -o 0 to -o 6. It also supports a --strip option to remove metadata (like comments or color profiles) for additional savings. Example:

oxipng -o 3 --strip all image.png

Batch Processing With Oxipng

To batch compress png terminal with oxipng, use a similar find command:

find . -name '*.png' -exec oxipng -o 3 --strip all {} +

Adjust the optimization level and stripping options based on your need for speed versus size reduction.

Combining Lossy and Lossless Compression

For maximum file size reduction, run pngquant first and a lossless optimizer second. This combines both savings: pngquant reduces color depth, then OptiPNG or oxipng recompresses the result losslessly. A typical workflow:

  1. Run pngquant with your chosen quality range.
  2. Run OptiPNG or oxipng on the pngquant output.

Example using pngquant then oxipng on a single file:

pngquant --quality=65-80 --ext .png --force image.png && oxipng -o 3 --strip all image.png

The && ensures the second command runs only if the first succeeds. For batch processing, chain the commands in a script or use a loop.

Batch Processing Examples

When working with many PNG files, batch commands save time. Below are examples for common tasks.

Batch Lossy Compression

This command processes all PNG files in the current directory, overwriting each with a compressed version:

for f in *.png; do pngquant --quality=65-80 --ext .png --force "$f"; done

Batch Lossless Optimization

Use the find command to recursively optimize all PNGs with OptiPNG:

find . -name '*.png' -exec optipng -o2 {} +

For oxipng with metadata stripping:

find . -name '*.png' -exec oxipng -o 3 --strip all {} +

Adjust the optimization level or quality values to suit your needs. Test on a few files first to see the size savings and processing time.

Choosing the Right Tool for the Job

Use pngquant when you can accept a small loss in color accuracy for a much smaller file, such as for web images or thumbnails. Use OptiPNG or oxipng when you need lossless compression (e.g., for archival or when pixel-perfect output is required). oxipng is faster on multi-core systems and offers metadata stripping, while OptiPNG provides more optimization levels. For the best results, combine both approaches. For more information on PNG compression techniques, visit compresspng.org.