pngquant

repository·main·Indexed 26 days ago

https://github.com/kornelski/pngquant

A high-quality PNG compressor that converts 24/32-bit PNG images to an efficient 8-bit indexed color format with alpha channels. Version 3.0.4 supports batch conversion, quality range configuration via --quality, speed adjustments from 1 to 11, and options for dithering control, metadata stripping, and color palette limitation.

Tokens
1.2K
Snippets
1
Records
10
Agent score
41%

What's inside pngquant

  1. Use pngquant for image compression

    main

    pngquant is a PNG compressor that reduces file sizes by converting images to an efficient 8-bit PNG format with an alpha channel. This typically results in files that are 60-80% smaller than 24/32-bit PNGs while remaining standards-compliant.

    Basic Usage

    • Batch conversion: Process multiple files at once using wildcards.
    • Stdin/Stdout: Chain with other Unix tools using pngquant -.

    To achieve even smaller file sizes, it is recommended to use pngquant in conjunction with tools like oxipng, ImageOptim, or zopflipng.

  2. Control dithering and color precision

    main

    Dithering Control

    • --nofs: Disables Floyd-Steinberg dithering entirely.
    • --floyd=0.5: Controls the level of dithering where 0 is none and 1 is full. Note that the = character is required (e.g., --floyd=0.5).

    Color Precision (--posterize bits)

    Reduces the precision of the palette by a specified number of bits. This is useful when images are intended for low-depth screens (e.g., 16-bit displays or ARGB444 textures).

  3. Configure compression quality with `--quality min-max`

    main

    Set a quality range using two numbers from 0 (worst) to 100 (perfect).

    • pngquant will use the minimum number of colors required to meet or exceed the max quality value.
    • If the resulting quality is below the min value, the image will not be saved (if outputting to stdout, the 24-bit original is returned) and the program exits with status code 99.

    Example: pngquant --quality=65-80 image.png

    pngquant --quality=65-80 image.png
  4. Set output filename and extensions

    main

    Custom Output Path (-o or --output)

    Specify a specific path for the converted file. Note that when using this option, you are restricted to a single input file.

    Custom File Extension (--ext)

    Set a custom suffix for the output filename. By default, pngquant uses -or8.png or -fs8.png.

    Warning: If you use --ext=.png --force, pngquant will overwrite the original input files in place. Use this with caution.

  5. Optimize compression speed and size

    main

    Speed/Quality Trade-off (--speed N)

    Adjust the balance between speed and compression quality using a value from 1 to 11:

    • 1: Slowest, highest quality, smallest files.
    • 11: Fastest, less consistent quality, lighter compression.
    • Default: 4. It is recommended to keep the default unless generating images in real-time (e.g., map tiles).

    Skip larger files (--skip-if-larger)

    Prevents writing the converted file if the compression does not actually reduce the file size.

    Strip metadata (--strip)

    Prevents the copying of optional PNG chunks (metadata).

  6. Reference: pngquant CLI flags and options

    main

    The following flags and options are available for the pngquant CLI:

    FlagOptionDescription
    -v--verboseEnable verbose logging
    -h--helpPrint help text
    -q--quietQuiet mode
    -f--forceForce compression even if quality is lower than requested
    --no-forceDisable force mode
    --orderedUse ordered dithering
    --nofsDisable Floyd-Steinberg dithering
    --skip-if-largerDo not overwrite if the output is larger than the input
    --stripStrip all metadata
    -V--versionPrint version information
    --floyd <val>Set Floyd-Steinberg dithering value (0.0-1.0)
    --ext <ext>Set output file extension
    -o--output <file>Specify output file (use - for stdout)
    -s--speed <N>Set speed (1-11)
    -Q--quality <min-max>Set quality range (0-100)
    --posterize <bits>Set posterization (0-4 bits)
    --map <file>Use a specific PNG as a color map
    --colors <N>Limit the number of colors (2-256)
  7. Control compression speed with --speed

    main

    Adjust the compression speed using a value between 1 (slowest) and 11 (fastest).

    • Values 1-9: Standard speed range.
    • Value 10: Enables fast compression.
    • Value 11: Enables fast compression and disables dithering (--floyd=0.0).
  8. Set output file or extension

    main

    You can specify how the output files are named using either --output or --ext. Note that these two options cannot be used at the same time.

    • --output <file>: Specifies a single output file. When using this, only one input file is allowed. Use - to write to stdout.
    • --ext <extension>: Specifies a new file extension to append to the original filename (e.g., --ext .webp). If not provided, pngquant defaults to -fs8.png (if Floyd-Steinberg dithering is used) or -or8.png (if ordered dithering is used).
  9. Limit colors and posterization

    main

    Use these options to reduce the color palette or simplify the image structure:

    • --colors <N>: Limits the number of colors in the output image. Must be between 2 and 256.
    • --posterize <bits>: Reduces the number of bits per color channel. Must be between 0 and 4 bits.
  10. Configure quality settings with --quality

    main

    Set the target quality range for the lossy compression. The quality values are numbers between 0 (lousy) and 100 (perfect).

    Supported formats:

    • N-M: No worse than N, no better than M (e.g., 60-80).
    • N-: No worse than N, perfect if possible (same as N-100).
    • -N: No better than N (same as 0-N).
    • N: Target quality N (internally sets range to N*0.9 to N).