LinuxCommandLibrary

pngquant

Compress PNG images lossily

TLDR

Compress a specific PNG as much as possible and write result to a new file

$ pngquant [path/to/file.png]
copy

Compress a specific PNG and override original
$ pngquant --ext .png --force [path/to/file.png]
copy

Try to compress a specific PNG with custom quality (skip if below the min value)
$ pngquant --quality [0-100] [path/to/file.png]
copy

Compress a specific PNG with the number of colors reduced to 64
$ pngquant [64] [path/to/file.png]
copy

Compress a specific PNG and skip if the file is larger than the original
$ pngquant --skip-if-larger [path/to/file.png]
copy

Compress a specific PNG and remove metadata
$ pngquant --strip [path/to/file.png]
copy

Compress a specific PNG and save it to the given path
$ pngquant [path/to/file.png] --output [path/to/file.png]
copy

Compress a specific PNG and show progress
$ pngquant --verbose [path/to/file.png]
copy

SYNOPSIS

pngquant [options] [file ...]
pngquant [options] - < input.png > output.png

PARAMETERS

file ...
    One or more input PNG files to process. If none specified, reads from standard input.

-
    Reads image data from standard input (stdin) and writes to standard output (stdout).

-o , --output
    Specifies the output file name. If omitted, pngquant appends `-fs8.png` or `-or8.png` to the input filename.

--quality min-max
    Sets the quality range for compression, e.g., `0-100`. `min` is the minimum acceptable quality, `max` is the target.

--speed N
    Controls the tradeoff between compression speed and quality (1-11). Higher speeds (e.g., 11) result in faster compression but potentially larger files; lower speeds (e.g., 1) are slower but yield better quality/smaller files.

--ext
    Sets the output file extension (e.g., `.png`). Useful with `--output` for custom naming conventions.

--force
    Overwrites existing output files without prompting.

--skip-if-larger
    Only saves the output file if it's smaller than the original input file.

--strip
    Strips all optional metadata (e.g., text chunks, EXIF, color profiles) from the output PNG.

--verbose
    Prints detailed information about the compression process to stderr.

--nofs
    Disables Floyd-Steinberg dithering. Can sometimes reduce file size further but may introduce visible banding.

--version
    Displays the pngquant version information.

--help
    Shows the help message and available options.

DESCRIPTION

pngquant is a powerful command-line utility designed for lossy compression of PNG images. It achieves significant file size reductions by converting 24-bit or 32-bit full-color PNGs into more efficient 8-bit paletted PNGs. The tool employs a sophisticated perceptual quantization algorithm that intelligently selects the optimal color palette for each image. This process aims to maintain high visual quality, even with a reduced color count, by prioritizing colors important to human perception. Its primary application is optimizing images for web delivery, resulting in faster loading times, reduced bandwidth consumption, and improved user experience. It's also widely used for preparing graphics for mobile applications and games where file size efficiency is critical. pngquant offers a robust balance between image quality and extreme file size reduction.

CAVEATS

Lossy Compression: pngquant performs lossy compression, meaning some original image data is permanently discarded. While designed to minimize visual impact, a degree of quality reduction is inherent, especially noticeable on images with a wide range of unique colors or subtle gradients.
Palette Limitation: The output is an 8-bit paletted PNG, limited to a maximum of 256 unique colors. Images with more than 256 distinct colors will have their color count reduced, which might lead to color shifts or banding.
Unsupported Features: Certain advanced PNG features like APNG (animated PNGs) are not supported and will result in a static image. Embedded ICC profiles are also typically stripped unless specific measures are taken.

INPUT AND OUTPUT HANDLING

pngquant can process individual files, multiple files, or integrate into pipelines using standard input/output. When processing multiple files, it typically creates a new file for each input, appending a suffix like `-fs8.png` unless an output name or extension is explicitly specified. Using `-` for standard input/output allows seamless integration with other command-line tools in a pipe.

BATCH PROCESSING

For optimizing a large number of images, pngquant is commonly used in conjunction with shell utilities like `find` and `xargs` to automate optimization tasks across entire directories. This makes it highly suitable for integration into automated build processes, content management systems, and other deployment workflows requiring optimized image assets.

HISTORY

pngquant was originally developed by Kornel LesiƄski. Since its inception, it has grown to become a widely adopted and essential tool for image optimization in web development and other media pipelines. Its development has consistently focused on pioneering efficient algorithms for perceptual quantization, allowing for impressive file size reductions without severely compromising visual fidelity. This continuous improvement in balancing speed, output quality, and compression effectiveness has solidified its position as a go-to utility for handling static PNG assets.

SEE ALSO

optipng(1), jpegoptim(1), gifsicle(1), convert(1), cwebp(1)

Copied to clipboard