Metadata-Version: 2.5
Name: lastpng
Version: 0.2.1
Summary: A vibe-coded, blazingly fast PNG compressor for Pillow
License-Expression: MIT
License-File: LICENSE
License-File: licenses/fdeflate-LICENSE-MIT
License-File: licenses/libdeflate-COPYING
Requires-Python: >=3.10
Requires-Dist: cffi>=1.17
Requires-Dist: pillow<=13.0.0,>=11
Description-Content-Type: text/markdown

# LastPNG for Pillow

LastPNG makes reading and saving PNG images in Pillow faster and optimizes file size by choosing the palette, bit depth, and color type automatically. Output remains standard PNG, preserving visible colors and transparency; RGB values in fully transparent pixels may change. Reading supports PNG and APNG, with Pillow handling metadata and animation composition.

## Installation

Requires Python ≥3.10 and Pillow ≥11, ≤13.0.0.

```sh
pip install lastpng
```

Wheels bundle the native libraries; no separate libdeflate or Windows VC Redistributable installation is needed.

## Usage

```python
from PIL import Image
import LastPNGImagePlugin

# Select LastPNG explicitly when saving.
with Image.open("input.png") as image:
    image.save("output.png", format="LASTPNG", level=3)

# Use Rust decoding and LastPNG encoding by default throughout the process.
LastPNGImagePlugin.register_as_default()
with Image.open("input.png") as image:
    image.save("output.png", level=3)
```

Use `register_as_default(encoder=False)` to enable only Rust decoding and keep the current PNG encoder.

Choose a compression level from 1 (fastest) to 6 (experimental, highest effort). The default is 3.

### Save options

Supports `pnginfo`, `icc_profile`, `transparency`, `dpi`, and `exif`. An ICC profile preserves the input color family.

In `pnginfo`, IHDR, PLTE, tRNS, IDAT, IEND, bKGD, hIST, sBIT, eXIf, and iCCP raise `ValueError`. LastPNG selects the output representation; use `exif=` for EXIF and `icc_profile=` for an uncompressed ICC profile.

For a unitless pixel aspect ratio or exact pixels-per-metre values, pass one `pHYs` chunk in `pnginfo`; it is written before IDAT. Combining it with `dpi` raises `ValueError`.

HDR `mDCV` (24 bytes) and `cLLI` (8 bytes) are accepted once each through `pnginfo` and written before PLTE and IDAT. `mDCV` requires `cICP`, in either input order.

## Limitations and behavior

### Formats and compatibility

Encoding supports single images in modes `1`, `L`, `I;16`, `P`, `RGB`, `LA`, and `RGBA`. APNG saving uses Pillow's encoder. Decoding supports Adam7 interlacing and APNG.

Supports CPython with Pillow major versions 11, 12, and 13. Unsupported versions raise an error when importing the plugin.

### Image access and animation

Keep the image unchanged until `save()` returns. Serialize loading, seeking, and pixel access on the same image object.

APNG decoding streams frames on demand, reading frame data once during forward playback, including gzip streams. Initialization may reread large headers; rewinding or retrying a failed native decode rereads the stream. Keep the file open while frames are needed.

Pillow's animation limitations apply, including unsupported `I;16` blending with `OP_OVER`. Reopen the image after errors in Pillow metadata processing, frame transitions, or composition.

### Damaged PNGs

With `PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True`, decoding tolerates early EOF, missing rows, and damaged compressed data. Complete rows (including Adam7 pass rows) are retained; unrecovered pixels remain zero or retain the existing APNG canvas. CRC and Adler-32 checks are disabled, but Pillow still validates headers, metadata, and APNG frame controls and sequences.

Recovered pixels may differ from Pillow's zlib decoder. Rust's structural checks and static PNG memory limits can reject files Pillow accepts. For APNG, Rust's internal allocation budget is disabled; Pillow controls destination image allocation.

Without truncated loading, corrupt static PNG IDAT checksums or a missing chunk header after frame data cause errors that Pillow may tolerate during `load()`. APNG fdAT CRC errors are accepted, as in Pillow.

## Building from source

A Rust toolchain and a C compiler are required. From the repository root:

```sh
python -m pip install ./python  # Install from the checkout.

# Build an sdist and wheel in python/dist/.
python -m pip install build
python -m build ./python
```
