High quality quantization for Pillow images.
Project description
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 |
---|---|---|---|
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')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hquant-0.1.0.tar.gz
.
File metadata
- Download URL: hquant-0.1.0.tar.gz
- Upload date:
- Size: 347.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7e071aa8828e46af36187f1afe405ee66a1cd346b49d14599e42f6fe5da40cc |
|
MD5 | 36fd4d150f747f1ab4a7a3fd233b93ce |
|
BLAKE2b-256 | 3ddf24d0c513565b6099dcd5bf9e5a01ac1877b311eda29dcde1d37747df7746 |
File details
Details for the file hquant-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: hquant-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce249b4626bcf8f12e74b9f0ae2a6a61536f3b54faf855e2e8d3784401a90c7f |
|
MD5 | 45abd1fab360cb26ea38e45d1b1a6bc7 |
|
BLAKE2b-256 | 55d2760a4141e7acf491a783ba2fa7e49cc0c828594d8e798ec53dc6e156b168 |