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
|
|
|
|
|
|
|
|
|
|
|
def test_sanity():
|
|
|
|
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")
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_libimagequant_quantize():
|
|
|
|
image = hopper()
|
2022-02-14 04:12:33 +03:00
|
|
|
if is_ppc64le():
|
|
|
|
libimagequant = parse_version(features.version_feature("libimagequant"))
|
|
|
|
if libimagequant < parse_version("4"):
|
|
|
|
pytest.skip("Fails with libimagequant earlier than 4.0.0 on ppc64le")
|
|
|
|
converted = image.quantize(100, Image.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
|
|
|
|
|
|
|
|
|
|
|
|
def test_octree_quantize():
|
|
|
|
image = hopper()
|
|
|
|
converted = image.quantize(100, Image.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
|
|
|
|
|
|
|
|
|
|
|
|
def test_rgba_quantize():
|
|
|
|
image = hopper("RGBA")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
image.quantize(method=0)
|
|
|
|
|
|
|
|
assert image.quantize().convert().mode == "RGBA"
|
|
|
|
|
|
|
|
|
|
|
|
def test_quantize():
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
def test_quantize_no_dither():
|
|
|
|
image = hopper()
|
|
|
|
with Image.open("Tests/images/caption_6_33_22.png") as palette:
|
|
|
|
palette = palette.convert("P")
|
|
|
|
|
|
|
|
converted = image.quantize(dither=0, 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
|
|
|
|
|
|
|
|
|
|
|
def test_quantize_dither_diff():
|
|
|
|
image = hopper()
|
|
|
|
with Image.open("Tests/images/caption_6_33_22.png") as palette:
|
|
|
|
palette = palette.convert("P")
|
|
|
|
|
|
|
|
dither = image.quantize(dither=1, palette=palette)
|
|
|
|
nodither = image.quantize(dither=0, palette=palette)
|
|
|
|
|
|
|
|
assert dither.tobytes() != nodither.tobytes()
|
2021-02-25 14:49:11 +03:00
|
|
|
|
|
|
|
|
2021-12-11 08:23:37 +03:00
|
|
|
def test_colors():
|
|
|
|
im = hopper()
|
|
|
|
colors = 2
|
|
|
|
converted = im.quantize(colors)
|
|
|
|
assert len(converted.palette.palette) == colors * len("RGB")
|
|
|
|
|
|
|
|
|
2021-02-25 14:49:11 +03:00
|
|
|
def test_transparent_colors_equal():
|
|
|
|
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",
|
|
|
|
(
|
|
|
|
(Image.MEDIANCUT, (0, 0, 0)),
|
|
|
|
(Image.MAXCOVERAGE, (0, 0, 0)),
|
|
|
|
(Image.FASTOCTREE, (0, 0, 0)),
|
|
|
|
(Image.FASTOCTREE, (0, 0, 0, 0)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_palette(method, color):
|
|
|
|
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]
|