Compressing a PNG file is useful when screenshots, interface assets, and diagrams need to upload faster, fit stricter attachment limits, or take less space in a repository without switching to another image format. PNG keeps sharp edges and transparency, so reducing the file size is often preferable to converting the image to JPEG.

In Linux, pngquant reduces PNG size by converting a full-color image to a smaller indexed-color PNG while preserving the alpha channel. By default it writes a new file beside the original with a suffix such as -fs8.png or -or8.png, which makes it easy to compare the compressed copy before replacing the source file.

The conversion is lossy, so gradients, photos, and artwork with subtle shading should be reviewed before the original file is overwritten. The command flow below was verified in a current Ubuntu environment, and the same pngquant options apply the same way on other Linux distributions that package the tool.

Steps to compress a PNG file with pngquant in Linux:

  1. Switch to the directory that contains the PNG file.
    $ cd /path/to/images

    Without –output or –ext, pngquant writes the compressed file in the current directory beside the source image.

  2. Check the current file size before compressing the image.
    $ ls -lh sample.png
    -rw-r--r-- 1 root root 3.9K Apr 14 04:29 sample.png

    The starting size makes it easier to confirm whether the reduced-color copy is worth keeping.

  3. Compress the PNG to a separate file and skip the write if the result would be larger.
    $ pngquant --skip-if-larger --strip sample.png

    –strip removes optional PNG metadata, and –skip-if-larger leaves the original untouched when compression does not produce a smaller file.

  4. Compare the original file with the compressed copy before deciding which one to keep.
    $ ls -lh sample.png sample-fs8.png
    -rw-r--r-- 1 root root 3.1K Apr 14 04:29 sample-fs8.png
    -rw-r--r-- 1 root root 3.9K Apr 14 04:29 sample.png

    Screenshots, diagrams, and interface assets usually compress well because they contain repeated colors and large flat areas.

  5. Write a custom output file with a narrower quality range when the default result is still larger than needed.
    $ pngquant --quality=60-80 --strip --output sample-quality.png sample.png
    $ ls -lh sample-quality.png
    -rw-r--r-- 1 root root 2.0K Apr 14 04:29 sample-quality.png

    –quality=min-max saves the file only when the result stays at or above the minimum quality threshold, and –output keeps the tuned result separate from the source image.

  6. Replace the original file only after the compressed result looks acceptable.
    $ pngquant --ext .png --force --skip-if-larger --strip sample.png
    $ ls -lh sample.png
    -rw-r--r-- 1 root root 3.1K Apr 14 04:29 sample.png

    –ext .png –force writes the reduced-color result back to the original path, so check the separate compressed copy first for banding, halos, or unwanted color shifts.