Skip to main content

hquant

High quality quantization for Pillow images.

Pillow uses Euclidean distance for color matching during quantization, which yields poor results when using RGB due to the nonlinearity of color perception. YCbCr is a better fit for Euclidean distance, but Pillow does not natively support quantization of such images.

This module improves the quantization by converting the images to YCbCr, and then tricking Pillow into thinking it's an RGB image so quantization works.

Samples

The following samples dither the original image to the classic 16 colors supported in all terminals, using RGB (as Pillow quantize method would normally use), CIELAB and YCbCr.

Original RGB CIELAB YCbCr
Original Lena Lena using RGB Lena using CIELAB Lena using YCbCr
Original Kobold Kobold using RGB Kobold using CIELAB Kobold using YCbCr

Usage example

from PIL import Image
import hquant

terminal_palette = bytes([
	# Primary 3-bit (8 colors). Unique representation!
	0x00, 0x00, 0x00,
	0x80, 0x00, 0x00,
	0x00, 0x80, 0x00,
	0x80, 0x80, 0x00,
	0x00, 0x00, 0x80,
	0x80, 0x00, 0x80,
	0x00, 0x80, 0x80,
	0xc0, 0xc0, 0xc0,

	# Equivalent "bright" versions of original 8 colors.
	0x80, 0x80, 0x80,
	0xff, 0x00, 0x00,
	0x00, 0xff, 0x00,
	0xff, 0xff, 0x00,
	0x00, 0x00, 0xff,
	0xff, 0x00, 0xff,
	0x00, 0xff, 0xff,
	0xff, 0xff, 0xff,
])

original = Image.open('lena.png')
dithered = hquant.quantize(original, terminal_palette)
dithered.save('dithered.png')

Release files for hquant 0.1.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 hquant 0.1.0
File Size Uploaded
hquant-0.1.0.tar.gz 347.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hquant 0.1.0
File Interpreter ABI Platform
hquant-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 351.2 kB

Release files / hquant-0.1.0.tar.gz

Download URL hquant-0.1.0.tar.gz
Size 347.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b7e071aa8828e46af36187f1afe405ee66a1cd346b49d14599e42f6fe5da40cc
BLAKE2b-256 checksum
How to use checksums
3ddf24d0c513565b6099dcd5bf9e5a01ac1877b311eda29dcde1d37747df7746
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.3

Release files / hquant-0.1.0-py3-none-any.whl

Download URL hquant-0.1.0-py3-none-any.whl
Size 3.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce249b4626bcf8f12e74b9f0ae2a6a61536f3b54faf855e2e8d3784401a90c7f
BLAKE2b-256 checksum
How to use checksums
55d2760a4141e7acf491a783ba2fa7e49cc0c828594d8e798ec53dc6e156b168
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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