2020-02-12 19:29:19 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2020-11-04 03:58:06 +03:00
|
|
|
from .helper import assert_image, assert_image_similar, hopper, is_ppc64le
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_sanity():
|
|
|
|
image = hopper()
|
|
|
|
converted = image.quantize()
|
|
|
|
assert_image(converted, "P", converted.size)
|
|
|
|
assert_image_similar(converted.convert("RGB"), image, 10)
|
|
|
|
|
|
|
|
image = hopper()
|
|
|
|
converted = image.quantize(palette=hopper("P"))
|
|
|
|
assert_image(converted, "P", converted.size)
|
|
|
|
assert_image_similar(converted.convert("RGB"), image, 60)
|
|
|
|
|
|
|
|
|
2020-11-04 03:58:06 +03:00
|
|
|
@pytest.mark.xfail(is_ppc64le(), reason="failing on ppc64le on GHA")
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_libimagequant_quantize():
|
|
|
|
image = hopper()
|
|
|
|
try:
|
|
|
|
converted = image.quantize(100, Image.LIBIMAGEQUANT)
|
2020-11-04 16:36:02 +03:00
|
|
|
except ValueError as ex: # pragma: no cover
|
2020-02-12 19:29:19 +03:00
|
|
|
if "dependency" in str(ex).lower():
|
|
|
|
pytest.skip("libimagequant support not available")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
assert_image(converted, "P", converted.size)
|
|
|
|
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)
|
|
|
|
assert_image(converted, "P", converted.size)
|
|
|
|
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()
|
|
|
|
assert_image(converted, "P", converted.size)
|
|
|
|
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)
|
|
|
|
assert_image(converted, "P", converted.size)
|
|
|
|
|
|
|
|
|
|
|
|
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()
|