2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from .helper import PillowTestCase, hopper
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImageQuantize(PillowTestCase):
|
|
|
|
def test_sanity(self):
|
2016-05-26 18:23:43 +03:00
|
|
|
image = hopper()
|
|
|
|
converted = image.quantize()
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assert_image(converted, "P", converted.size)
|
|
|
|
self.assert_image_similar(converted.convert("RGB"), image, 10)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2016-05-26 18:23:43 +03:00
|
|
|
image = hopper()
|
2019-06-13 18:54:24 +03:00
|
|
|
converted = image.quantize(palette=hopper("P"))
|
|
|
|
self.assert_image(converted, "P", converted.size)
|
|
|
|
self.assert_image_similar(converted.convert("RGB"), image, 60)
|
2013-03-11 23:33:04 +04:00
|
|
|
|
2016-05-05 22:36:45 +03:00
|
|
|
def test_libimagequant_quantize(self):
|
2016-05-26 18:23:43 +03:00
|
|
|
image = hopper()
|
2016-05-06 22:06:58 +03:00
|
|
|
try:
|
2016-05-26 18:23:43 +03:00
|
|
|
converted = image.quantize(100, Image.LIBIMAGEQUANT)
|
2016-05-06 22:06:58 +03:00
|
|
|
except ValueError as ex:
|
2019-06-13 18:54:24 +03:00
|
|
|
if "dependency" in str(ex).lower():
|
|
|
|
self.skipTest("libimagequant support not available")
|
2016-05-06 22:06:58 +03:00
|
|
|
else:
|
|
|
|
raise
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assert_image(converted, "P", converted.size)
|
|
|
|
self.assert_image_similar(converted.convert("RGB"), image, 15)
|
2018-01-17 14:01:37 +03:00
|
|
|
self.assertEqual(len(converted.getcolors()), 100)
|
2016-05-05 22:36:45 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_octree_quantize(self):
|
2016-05-26 18:23:43 +03:00
|
|
|
image = hopper()
|
|
|
|
converted = image.quantize(100, Image.FASTOCTREE)
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assert_image(converted, "P", converted.size)
|
|
|
|
self.assert_image_similar(converted.convert("RGB"), image, 20)
|
2018-01-17 14:01:37 +03:00
|
|
|
self.assertEqual(len(converted.getcolors()), 100)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_rgba_quantize(self):
|
2019-06-13 18:54:24 +03:00
|
|
|
image = hopper("RGBA")
|
2018-06-08 12:17:48 +03:00
|
|
|
self.assertRaises(ValueError, image.quantize, method=0)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2019-03-16 05:36:58 +03:00
|
|
|
self.assertEqual(image.quantize().convert().mode, "RGBA")
|
|
|
|
|
2015-08-22 16:03:11 +03:00
|
|
|
def test_quantize(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/caption_6_33_22.png") as image:
|
|
|
|
image = image.convert("RGB")
|
2016-05-26 18:23:43 +03:00
|
|
|
converted = image.quantize()
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assert_image(converted, "P", converted.size)
|
|
|
|
self.assert_image_similar(converted.convert("RGB"), image, 1)
|
2019-03-09 02:36:13 +03:00
|
|
|
|
|
|
|
def test_quantize_no_dither(self):
|
|
|
|
image = hopper()
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/caption_6_33_22.png") as palette:
|
|
|
|
palette = palette.convert("P")
|
2019-03-09 02:36:13 +03:00
|
|
|
|
|
|
|
converted = image.quantize(dither=0, palette=palette)
|
2019-06-13 18:54:24 +03:00
|
|
|
self.assert_image(converted, "P", converted.size)
|
2019-03-09 02:36:13 +03:00
|
|
|
|
|
|
|
def test_quantize_dither_diff(self):
|
|
|
|
image = hopper()
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/caption_6_33_22.png") as palette:
|
|
|
|
palette = palette.convert("P")
|
2019-03-09 02:36:13 +03:00
|
|
|
|
|
|
|
dither = image.quantize(dither=1, palette=palette)
|
|
|
|
nodither = image.quantize(dither=0, palette=palette)
|
|
|
|
|
|
|
|
self.assertNotEqual(dither.tobytes(), nodither.tobytes())
|