Oxipng

Oxipng is a powerful, multithreaded lossless PNG/APNG compression optimizer. It can be used via a command-line interface or as a library in other Rust programs, making it a versatile tool for developers and users who need efficient image optimization.

Overview

Oxipng excels in compressing images without any loss of quality, providing a significant reduction in file size. This makes it particularly useful for web developers and anyone looking to optimize images for faster loading times and reduced bandwidth usage.

Key Features

  • Multithreaded Compression: Utilizes multiple CPU cores for faster image processing.
  • Lossless Optimization: Reduces file size without any loss of image quality.
  • Command-Line Interface: Easy to use with various options for customization.
  • Library Support: Can be integrated into other Rust programs for seamless image optimization.

Installation

Oxipng can be installed on various operating systems, including Windows, MacOS, and Linux. Here are some installation methods:

Windows

Download the latest release from the Releases page on GitHub.

MacOS and Linux

Install from your distribution's package repository if available. For example:

brew install oxipng  # MacOS
sudo apt-get install oxipng  # Debian/Ubuntu

Alternatively, install Oxipng using Cargo, Rust's package manager:

cargo install oxipng

Building from Source

To build Oxipng from source, you need the latest stable or nightly Rust:

git clone https://github.com/shssoichiro/oxipng.git
cd oxipng
cargo build --release
cp target/release/oxipng /usr/local/bin

Usage

Oxipng is primarily a command-line utility. Here's an example command to optimize images for web use:

oxipng -o 4 --strip safe --alpha *.png

Commonly Used Options

  • Optimization Level: -o 0 to -o 6 (or -o max). Higher levels provide better compression but take longer. The default is -o 2.
  • Strip Metadata: --strip [safe,all] removes metadata to save space. -s can be used as a shorthand for --strip safe.
  • Alpha Channel Optimization: --alpha optimizes images with transparency, altering the color values of fully transparent pixels for better compression.

For a full list of options, run:

oxipng --help

Git Integration

Pre-Commit

Integrate Oxipng into your Git workflow using [pre-commit]:

repos:
  - repo: https://github.com/shssoichiro/oxipng
    rev: v9.0.0
    hooks:
      - id: oxipng
        args: ["-o", "4", "--strip", "safe", "--alpha"]

Trunk

[Trunk] is an extendable superlinter that can use Oxipng to optimize PNG files in a Git repository:

trunk check enable oxipng

For more detailed setup instructions, refer to the Trunk documentation.

Library Usage

Oxipng can also be used as a library in other Rust projects. Add it to your Cargo.toml:

[dependencies]
oxipng = "9.0"

Then, use it in your project:

extern crate oxipng;
use oxipng::optimize;

For more information, refer to the Oxipng library documentation.

Benchmarks

Oxipng has been benchmarked against OptiPNG, demonstrating significantly faster performance. Here are some benchmark results:

Benchmark 1: ./target/release/oxipng -P ./tests/files/rgb_16_should_be_grayscale_8.png
  Time (mean ± σ):      59.6 ms ±   7.7 ms    [User: 77.4 ms, System: 3.6 ms]
  Range (min … max):    53.3 ms …  89.9 ms    32 runs

Benchmark 2: optipng -simulate ./tests/files/rgb_16_should_be_grayscale_8.png
  Time (mean ± σ):     132.4 ms ±   0.8 ms    [User: 132.5 ms, System: 0.6 ms]
  Range (min … max):   131.8 ms … 134.4 ms    22 runs

Summary
  ./target/release/oxipng -P ./tests/files/rgb_16_should_be_grayscale_8.png ran
    2.22 ± 0.29 times faster than optipng -simulate ./tests/files/rgb_16_should_be_grayscale_8.png

For more benchmark results and details, visit the Oxipng GitHub repository.

History

Oxipng began as a rewrite of the OptiPNG project, focusing on adding multithreading support and utilizing Rust for a more modern and safer codebase. While similar, Oxipng is not a drop-in replacement for OptiPNG.

Similar Projects