2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
import pytest
|
2022-02-14 04:12:33 +03:00
|
|
|
from packaging.version import parse as parse_version
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2022-02-14 04:12:33 +03:00
|
|
|
from PIL import Image, features
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-02-14 04:12:33 +03:00
|
|
|
from .helper import assert_image_similar, hopper, is_ppc64le, skip_unless_feature
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_sanity() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
image = hopper()
|
|
|
|
converted = image.quantize()
|
2021-12-26 05:27:41 +03:00
|
|
|
assert converted.mode == "P"
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_similar(converted.convert("RGB"), image, 10)
|
|
|
|
|
|
|
|
image = hopper()
|
|
|
|
converted = image.quantize(palette=hopper("P"))
|
2021-12-26 05:27:41 +03:00
|
|
|
assert converted.mode == "P"
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_similar(converted.convert("RGB"), image, 60)
|
|
|
|
|
|
|
|
|
2022-02-14 04:12:33 +03:00
|
|
|
@skip_unless_feature("libimagequant")
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_libimagequant_quantize() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
image = hopper()
|
2022-02-14 04:12:33 +03:00
|
|
|
if is_ppc64le():
|
2024-05-30 05:00:50 +03:00
|
|
|
version = features.version_feature("libimagequant")
|
|
|
|
assert version is not None
|
|
|
|
if parse_version(version) < parse_version("4"):
|
2022-02-14 04:12:33 +03:00
|
|
|
pytest.skip("Fails with libimagequant earlier than 4.0.0 on ppc64le")
|
2022-02-15 00:12:28 +03:00
|
|
|
converted = image.quantize(100, Image.Quantize.LIBIMAGEQUANT)
|
2021-12-26 05:27:41 +03:00
|
|
|
assert converted.mode == "P"
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_similar(converted.convert("RGB"), image, 15)
|
|
|
|
assert len(converted.getcolors()) == 100
|
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_octree_quantize() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
image = hopper()
|
2022-01-15 01:02:31 +03:00
|
|
|
converted = image.quantize(100, Image.Quantize.FASTOCTREE)
|
2021-12-26 05:27:41 +03:00
|
|
|
assert converted.mode == "P"
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_similar(converted.convert("RGB"), image, 20)
|
|
|
|
assert len(converted.getcolors()) == 100
|
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_rgba_quantize() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
image = hopper("RGBA")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
image.quantize(method=0)
|
|
|
|
|
|
|
|
assert image.quantize().convert().mode == "RGBA"
|
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_quantize() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open("Tests/images/caption_6_33_22.png") as image:
|
|
|
|
image = image.convert("RGB")
|
|
|
|
converted = image.quantize()
|
2021-12-26 05:27:41 +03:00
|
|
|
assert converted.mode == "P"
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_similar(converted.convert("RGB"), image, 1)
|
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_quantize_no_dither() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
image = hopper()
|
|
|
|
with Image.open("Tests/images/caption_6_33_22.png") as palette:
|
|
|
|
palette = palette.convert("P")
|
|
|
|
|
2022-02-19 02:49:23 +03:00
|
|
|
converted = image.quantize(dither=Image.Dither.NONE, palette=palette)
|
2021-12-26 05:27:41 +03:00
|
|
|
assert converted.mode == "P"
|
2021-08-30 17:33:10 +03:00
|
|
|
assert converted.palette.palette == palette.palette.palette
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_quantize_no_dither2() -> None:
|
2022-06-20 02:18:05 +03:00
|
|
|
im = Image.new("RGB", (9, 1))
|
2023-11-06 16:13:47 +03:00
|
|
|
im.putdata([(p,) * 3 for p in range(0, 36, 4)])
|
2022-06-20 02:18:05 +03:00
|
|
|
|
|
|
|
palette = Image.new("P", (1, 1))
|
|
|
|
data = (0, 0, 0, 32, 32, 32)
|
|
|
|
palette.putpalette(data)
|
|
|
|
quantized = im.quantize(dither=Image.Dither.NONE, palette=palette)
|
|
|
|
|
|
|
|
assert tuple(quantized.palette.palette) == data
|
|
|
|
|
|
|
|
px = quantized.load()
|
|
|
|
for x in range(9):
|
|
|
|
assert px[x, 0] == (0 if x < 5 else 1)
|
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_quantize_dither_diff() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
image = hopper()
|
|
|
|
with Image.open("Tests/images/caption_6_33_22.png") as palette:
|
|
|
|
palette = palette.convert("P")
|
|
|
|
|
2022-02-19 02:49:23 +03:00
|
|
|
dither = image.quantize(dither=Image.Dither.FLOYDSTEINBERG, palette=palette)
|
|
|
|
nodither = image.quantize(dither=Image.Dither.NONE, palette=palette)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
assert dither.tobytes() != nodither.tobytes()
|
2021-02-25 14:49:11 +03:00
|
|
|
|
|
|
|
|
2024-03-22 09:36:04 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"method", (Image.Quantize.MEDIANCUT, Image.Quantize.MAXCOVERAGE)
|
|
|
|
)
|
2024-06-09 08:16:17 +03:00
|
|
|
def test_quantize_kmeans(method: Image.Quantize) -> None:
|
2024-03-22 09:36:04 +03:00
|
|
|
im = hopper()
|
2024-03-22 15:53:07 +03:00
|
|
|
no_kmeans = im.quantize(kmeans=0, method=method)
|
|
|
|
kmeans = im.quantize(kmeans=1, method=method)
|
|
|
|
assert kmeans.tobytes() != no_kmeans.tobytes()
|
|
|
|
|
2024-03-22 09:36:04 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.quantize(kmeans=-1, method=method)
|
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_colors() -> None:
|
2021-12-11 08:23:37 +03:00
|
|
|
im = hopper()
|
|
|
|
colors = 2
|
|
|
|
converted = im.quantize(colors)
|
|
|
|
assert len(converted.palette.palette) == colors * len("RGB")
|
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_transparent_colors_equal() -> None:
|
2021-02-25 14:49:11 +03:00
|
|
|
im = Image.new("RGBA", (1, 2), (0, 0, 0, 0))
|
|
|
|
px = im.load()
|
|
|
|
px[0, 1] = (255, 255, 255, 0)
|
|
|
|
|
|
|
|
converted = im.quantize()
|
|
|
|
converted_px = converted.load()
|
|
|
|
assert converted_px[0, 0] == converted_px[0, 1]
|
2021-12-11 08:07:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"method, color",
|
|
|
|
(
|
2022-01-15 01:02:31 +03:00
|
|
|
(Image.Quantize.MEDIANCUT, (0, 0, 0)),
|
|
|
|
(Image.Quantize.MAXCOVERAGE, (0, 0, 0)),
|
|
|
|
(Image.Quantize.FASTOCTREE, (0, 0, 0)),
|
|
|
|
(Image.Quantize.FASTOCTREE, (0, 0, 0, 0)),
|
2021-12-11 08:07:45 +03:00
|
|
|
),
|
|
|
|
)
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_palette(method: Image.Quantize, color: tuple[int, ...]) -> None:
|
2021-12-11 08:07:45 +03:00
|
|
|
im = Image.new("RGBA" if len(color) == 4 else "RGB", (1, 1), color)
|
|
|
|
|
|
|
|
converted = im.quantize(method=method)
|
|
|
|
converted_px = converted.load()
|
|
|
|
assert converted_px[0, 0] == converted.palette.colors[color]
|
2022-02-16 03:01:00 +03:00
|
|
|
|
|
|
|
|
2024-01-25 14:18:46 +03:00
|
|
|
def test_small_palette() -> None:
|
2022-02-16 03:01:00 +03:00
|
|
|
# Arrange
|
|
|
|
im = hopper()
|
|
|
|
|
|
|
|
colors = (255, 0, 0, 0, 0, 255)
|
|
|
|
p = Image.new("P", (1, 1))
|
|
|
|
p.putpalette(colors)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
im = im.quantize(palette=p)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(im.getcolors()) == 2
|