LastPNG is a Rust PNG encoder with six compression levels, a C API, and a Pillow plugin. It can change the palette, bit depth, and color type to save space. Alpha and visible colors are preserved. RGB values in fully transparent pixels may change.
| Preset | Rust level | When to use it |
|---|---|---|
| 1 | Fastest |
Minimize encoding time. |
| 2 | Fast |
Favor speed while producing smaller files than level 1 on this corpus. |
| 3 | Balanced |
Start here for a balance between encoding time and file size. |
| 4 | High |
Favor smaller files over encoding speed. |
| 5 | Higher |
Use for offline encoding when file size matters more than speed. |
| 6 | MaxEffort |
Experimental, still in early development. Smallest LastPNG output on this corpus. |
The Practical frontier consists of Pareto-optimal presets that reduce file size by at least 1% per doubling of total encoding time. Candidates are considered from fastest to slowest, with each compared against the last retained preset. Encode (s) is the total encoding time across all images. MB is the total output size in decimal megabytes.
The benchmark measures in-memory PNG encoding on an Apple M4 Pro using a 1 GB test corpus of 4,079 images. Each encoded image is decoded and verified against the original.
- rust-png is the Rust
pngcrate (0.18.1). - png-zlib is libpng 1.6.59 built with zlib 1.4. Preset 6 selects zlib compression level 6.
- png-ng is libpng 1.6.59 built with zlib-ng 2.3.3 for DEFLATE compression.
- oxipng is the Rust PNG optimizer (10.2.1), called through its raw-image API.
- zopflipng is the ZopfliPNG optimizer. It takes PNG bytes as input, so its timing includes decoding.
On this corpus, LastPNG level 1 is both faster and smaller than Rust's png fastest preset. Level 2 is over 12× faster than libpng with zlib at level 6, with comparable output size. Level 3 is about 10× faster than libpng with zlib-ng at level 9, producing roughly 6% smaller files. LastPNG outperforms OxiPNG on both time and size only at OxiPNG level 0. Higher OxiPNG levels use computationally expensive algorithms for which LastPNG does not yet have faster alternatives.
Practical frontier:
| Encoder | Preset | Encode (s) | MB |
|---|---|---|---|
| lastpng | 1 | 2.94 | 1280 |
| rust-png | fast | 6.81 | 1167 |
| lastpng | 2 | 11.5 | 914 |
| lastpng | 3 | 26.5 | 837 |
| lastpng | 4 | 66.8 | 817 |
| lastpng | 5 | 278 | 777 |
| oxipng | 1 | 386 | 771 |
All encoders below are compared on the same 4,035 images supported by every format.
- webp is libwebp in lossless mode, using presets equivalent to
cwebp -z N. - qoi is the reference Quite OK Image encoder; the format is lossless by design.
- jxl is libjxl in lossless JPEG XL mode, using the original color space for coding and retaining the source sample depth.
- avif-aom is libavif with AOM in lossless mode. It uses reversible YCgCo-Re conversion (8-bit RGB to 10-bit, full-range 4:4:4) instead of the default GBR/identity conversion to produce smaller files while preserving the original RGB values exactly.
LastPNG level 1 is about 2.4× faster than QOI at nearly the same output size. Lossless JPEG XL effort 1 takes about 1.5× as long as LastPNG level 2 but produces 10% smaller files; it is both faster and smaller than LastPNG level 3. Lossless WebP occupies much of the middle of the practical frontier, while JPEG XL efforts 7–9 produce the smallest files shown. Lossless AVIF is uncompetitive on this corpus: preset 3 takes about 18× as long as LastPNG level 6 for just 0.2% smaller output. None of the AVIF presets shown reaches the practical frontier.
Practical frontier:
| Encoder | Preset | Encode (s) | MB |
|---|---|---|---|
| lastpng | 1 | 2.81 | 1129 |
| lastpng | 2 | 10.5 | 789 |
| jxl | 1 | 15.5 | 710 |
| webp | 1 | 127 | 549 |
| webp | 2 | 176 | 531 |
| webp | 3 | 221 | 514 |
| webp | 4 | 234 | 514 |
| webp | 5 | 254 | 512 |
| webp | 6 | 320 | 509 |
| jxl | 7 | 914 | 487 |
| jxl | 8 | 1878 | 476 |
| jxl | 9 | 4690 | 469 |
Add the crate with cargo add lastpng. Encoder accepts decoded pixels from a contiguous buffer or separate rows and writes to any std::io::Write, including files and memory buffers. Construction selects the PNG representation; compression happens when writing.
Encode a packed RGB buffer:
use lastpng::{BitDepth, ColorType, Encoder, Level, PixelFormat, PixelLayout};
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let pixels = [255, 0, 0, 0, 128, 255];
let format = PixelFormat::new(ColorType::Rgb, BitDepth::Eight, PixelLayout::Packed);
let encoder = Encoder::from_buffer(2, 1, format, &pixels, Level::Balanced)?;
encoder.write_png(File::create("output.png")?)?;
Ok(())
}Encode separately stored RGBA rows without joining them into a single buffer:
use lastpng::{BitDepth, ColorType, Encoder, Level, PixelFormat, PixelLayout};
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let rows: &[&[u8]] = &[&[255, 0, 0, 255], &[0, 128, 255, 128]];
let format = PixelFormat::new(ColorType::Rgba, BitDepth::Eight, PixelLayout::Packed);
let encoder = Encoder::from_rows(1, 2, format, rows, Level::Balanced)?;
encoder.write_png(File::create("output.png")?)?;
Ok(())
}Rows are supplied top to bottom. Packed uses PNG sample packing, with big-endian 16-bit samples. Other layouts support padded RGB/LA, byte-per-pixel bilevel, and little-endian 16-bit grayscale inputs. The encoder borrows the input until writing finishes, and each write method consumes it.
For custom metadata handling, inspect output_info() and use write_image_chunks() to write PLTE, tRNS, and IDAT chunks; the caller supplies the remaining PNG structure. Set EncoderOptions::preserve_color_space when embedding an ICC profile for the input color space.
Build with cargo build --release and link against lastpng in target/release. Pass a contiguous pixel buffer or an array of row pointers. Keep pixel, palette, and transparency data alive and unchanged until writing finishes.
Use lastpng_encoder_write_png for a complete file. To write your own metadata, query lastpng_encoder_output_info for the header and use lastpng_encoder_write_image_chunks for the palette, transparency, and image data. Both write functions consume the encoder. See the C API and callback contract.
Install with python -m pip install lastpng, then import the plugin to register the LASTPNG save format.
from PIL import Image
import LastPNGImagePlugin
with Image.open("input.png") as image:
image.save("output.png", format="LASTPNG", level=3)See the Pillow plugin documentation.