2014-09-05 14:03:56 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImageQuantize(PillowTestCase):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
2016-05-26 18:23:43 +03:00
|
|
|
image = hopper()
|
|
|
|
converted = image.quantize()
|
|
|
|
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()
|
|
|
|
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:
|
|
|
|
if 'dependency' in str(ex).lower():
|
2016-05-07 00:29:53 +03:00
|
|
|
self.skipTest('libimagequant support not available')
|
2016-05-06 22:06:58 +03:00
|
|
|
else:
|
|
|
|
raise
|
2016-05-26 18:23:43 +03:00
|
|
|
self.assert_image(converted, 'P', converted.size)
|
|
|
|
self.assert_image_similar(converted.convert('RGB'), image, 15)
|
|
|
|
assert 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)
|
|
|
|
self.assert_image(converted, 'P', converted.size)
|
|
|
|
self.assert_image_similar(converted.convert('RGB'), image, 20)
|
|
|
|
assert len(converted.getcolors()) == 100
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_rgba_quantize(self):
|
2016-05-26 18:23:43 +03:00
|
|
|
image = hopper('RGBA')
|
|
|
|
image.quantize()
|
|
|
|
self.assertRaises(Exception, lambda: image.quantize(method=0))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-08-22 16:03:11 +03:00
|
|
|
def test_quantize(self):
|
2016-05-26 18:23:43 +03:00
|
|
|
image = Image.open('Tests/images/caption_6_33_22.png').convert('RGB')
|
|
|
|
converted = image.quantize()
|
|
|
|
self.assert_image(converted, 'P', converted.size)
|
|
|
|
self.assert_image_similar(converted.convert('RGB'), image, 1)
|
2015-08-22 16:03:11 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|