Skip to main content

libimgquant for Python

Converts RGBA images to 8-bit palette images with alpha, for small PNGs and GIFs, like pngquant. Pure Rust underneath, BSD-licensed, with no runtime dependencies.

libimgquant 3 is a Rust rewrite of libimagequant 2.4.1, the last BSD-licensed release of the library behind pngquant, improved with published color quantization research: a variance-based median cut, Hartigan refinement after k-means, an exact nearest-color search, an OKLab-shaped color difference and a palette order that makes PNGs smaller. profile="compat" reproduces 2.4.1's algorithms instead, apart from a few bug fixes. The Rust crate's README describes the algorithms and compares them with libimagequant 2.4.1 and imagequant 4.4.1.

  • BSD-2-Clause, no GPL code: usable in closed-source and commercial software.
  • One wheel per platform for CPython 3.9 and newer (stable ABI, abi3).
  • Multi-threaded, and every function releases the GIL while it works.
  • Typed: ships type hints (py.typed).

Install

pip install libimgquant
pip install "libimgquant[pillow]"   # also installs Pillow, for quantize_image()

From a checkout of the repository, with a Rust toolchain installed: pip install ./python (maturin builds the wheel).

Compress a PNG file

import libimgquant

with open("photo.png", "rb") as f:
    data = f.read()

small = libimgquant.compress_png(data, quality=(65, 90))
with open("photo-fs8.png", "wb") as f:
    f.write(small)

compress_png reads any PNG and writes a palette PNG with the same dimensions, gamma and ICC color profile, the smallest bit depth, a tRNS chunk only as long as needed, and the smaller of a filtered and an unfiltered encoding.

Quantize pixels

quantize takes width * height RGBA pixels as any bytes-like object (width * height * 4 bytes, row by row) and returns the palette and one palette index per pixel:

import libimgquant

width, height = 64, 48
pixels = bytes(
    v
    for y in range(height)
    for x in range(width)
    for v in (x * 4, y * 5, 128, 100 if y < 8 else 255)
)

result = libimgquant.quantize(pixels, width, height, max_colors=64)
result.palette   # [(r, g, b, a), ...], at most 64 entries, translucent entries first
result.indices   # bytes, one palette index per pixel
result.quality   # quality reached, 0-100

numpy

A uint8 array of shape (height, width, 4) works as it is:

import numpy as np
import libimgquant

rgba = np.zeros((480, 640, 4), dtype=np.uint8)  # your image
result = libimgquant.quantize(rgba, 640, 480)

palette = np.array(result.palette, dtype=np.uint8)              # (colors, 4)
indices = np.frombuffer(result.indices, np.uint8).reshape(480, 640)
restored = palette[indices]                                      # (480, 640, 4)

Pillow

quantize_image takes a Pillow image in any mode, so any format Pillow opens (JPEG, WebP, GIF, ...) works, and returns a mode "P" image. The palette's alpha values go to info["transparency"], so saving it as PNG keeps transparency:

from PIL import Image
import libimgquant

img = Image.open("sprite.png")
small = libimgquant.quantize_image(img, max_colors=128, dithering=0.5)
small.save("sprite-128.png", optimize=True)

Pillow is imported only when quantize_image is called; the other functions don't need it.

One palette for many images

For an animated GIF, a sprite sheet or a tile set, quantize_frames computes one palette for several images of the same size and remaps each of them to it. Colors that appear in only one frame still get palette entries.

palette, frame_indices = libimgquant.quantize_frames(frames, width, height, max_colors=255)
# frame_indices[i]: the indices of frames[i] into the shared palette

Options

All functions take the same keyword-only options:

Option Default Meaning
max_colors 256 At most this many palette entries, 2–256.
speed 3 1 (slowest, best) to 10 (fastest). With profile="improved", speeds 1–7 also order the palette for smaller PNGs (not in quantize_frames, whose palette is shared).
quality None Like JPEG quality, 0–100. An int is the target; (minimum, target) also sets a minimum. The fewest colors that reach the target are used; below the minimum, QualityTooLowError is raised.
dithering 1.0 Floyd-Steinberg dithering, 0 (none) to 1 (full).
profile "improved" "improved": the version 3 algorithms. "compat": libimagequant 2.4.1's algorithms, step by step, apart from a few bug fixes.

Errors

  • ValueError: an option is out of range, the pixel buffer has the wrong size or is not 8-bit, or the PNG is invalid.
  • libimgquant.QualityTooLowError (a ValueError): the minimum quality can't be reached with max_colors colors. Keep the original image in that case, as pngquant does.
  • TypeError: the pixels are not a bytes-like object.
try:
    small = libimgquant.compress_png(data, quality=(80, 95))
except libimgquant.QualityTooLowError:
    small = data  # not worth converting

Threads

Quantization runs on all cores (a rayon thread pool), and the GIL is released while it runs, so other Python threads keep working and several images can be converted at once from a ThreadPoolExecutor. The output does not depend on the number of threads.

License

BSD-2-Clause, plus the original permissive notice by Jef Poskanzer. See the LICENSE and NOTICE files in the package. The wheels and the sdist also contain THIRD-PARTY-LICENSES.txt, the licenses of the Rust crates compiled into the extension. The Rust library is libimgquant on crates.io.

Metadata

Release files for libimgquant 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for libimgquant 3.0.0
File Size Uploaded Provenance Actions
libimgquant-3.0.0.tar.gz 162.8 kB With provenance Details for libimgquant-3.0.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for libimgquant 3.0.0
File Actions
libimgquant-3.0.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for libimgquant-3.0.0-cp39-abi3-win_amd64.whl
libimgquant-3.0.0-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details for libimgquant-3.0.0-cp39-abi3-musllinux_1_2_x86_64.whl
libimgquant-3.0.0-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details for libimgquant-3.0.0-cp39-abi3-musllinux_1_2_aarch64.whl
libimgquant-3.0.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for libimgquant-3.0.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
libimgquant-3.0.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for libimgquant-3.0.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
libimgquant-3.0.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for libimgquant-3.0.0-cp39-abi3-macosx_11_0_arm64.whl
libimgquant-3.0.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for libimgquant-3.0.0-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 4.4 MB

Release files / libimgquant-3.0.0.tar.gz

Download URL libimgquant-3.0.0.tar.gz
Size 162.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4e1b085ccefb57e040fa803f323587f70d54e7816eb47f8b4bd6e24f8c0c5925
BLAKE2b-256 checksum
How to use checksums
e270d781f568627360ca56ea61c0222dec6850034ca931968d4b09d4b086ae2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / libimgquant-3.0.0-cp39-abi3-win_amd64.whl

Download URL libimgquant-3.0.0-cp39-abi3-win_amd64.whl
Size 526.4 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
25eccdb2b346802358ca3495f844d305e44d36eca8aaaf813a26870598e69f98
BLAKE2b-256 checksum
How to use checksums
4ed3f20380a095c399e329052b941abf59f0663a59da7e10db283617701a8fd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / libimgquant-3.0.0-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL libimgquant-3.0.0-cp39-abi3-musllinux_1_2_x86_64.whl
Size 791.2 kB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
77470aeef0421b997eeb35130de77bcce068e2f2eb0cf681a27ac3b2e8dea6f1
BLAKE2b-256 checksum
How to use checksums
6d40f73b3cc4860ef0660e32f97412aa7d432c23b0ca6b33abd7b8a815f5f0d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / libimgquant-3.0.0-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL libimgquant-3.0.0-cp39-abi3-musllinux_1_2_aarch64.whl
Size 725.6 kB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
bebedff6c38c18e13fbf7f88bb0fba4a773ef7844b7696635424d23a8480de3e
BLAKE2b-256 checksum
How to use checksums
1a6913e2a4d5076b9d467b809a1110f691fbc85072ce6d6dbf1641899e6af3ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / libimgquant-3.0.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL libimgquant-3.0.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 581.1 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
ee0cc3080c130d205f5f340be13a5645c1b3c62ce89832b9cf1b20cf257c62af
BLAKE2b-256 checksum
How to use checksums
cdd2009b6b392764790a86ab7837cb27579ebc17da7a4935c7aafd2cd383eb97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / libimgquant-3.0.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL libimgquant-3.0.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 547.2 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
bcf5e11da78fa115125b2b4a9f1a5957a4755e01e357f704c9dd19d46febcc0d
BLAKE2b-256 checksum
How to use checksums
4137781a3fbc61580ef51c499aac6c6a98f99fe921340a5058363c11cc7bdb29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / libimgquant-3.0.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL libimgquant-3.0.0-cp39-abi3-macosx_11_0_arm64.whl
Size 518.5 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
295a7cd8372fb1fac9514516b5d9f50e2fd5316ba5947d157305c9b4c326ab9f
BLAKE2b-256 checksum
How to use checksums
a0d86696635c5c1cba327c3a2839fd105d35368649daab9900e998d9aa8736b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / libimgquant-3.0.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL libimgquant-3.0.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 545.2 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
7c3c0718fce6207fee59ebe29aba70fa0bebd7013904086bb0cf387d4a297871
BLAKE2b-256 checksum
How to use checksums
56e30d68d26903b6d698d5fd726b1b9a32b3443a7181a3591466a7d20318eb33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Security for libimgquant 3.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 8 files in this release have been uploaded with provenance metadata.

Select from 8 files

Attestation for libimgquant-3.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.0.0 This release

8 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page