Add perceptive comparisons to quantization tests

This commit is contained in:
rr- 2016-05-26 17:23:43 +02:00
parent 0430a4a834
commit db989ad99b

View File

@ -6,45 +6,46 @@ from PIL import Image
class TestImageQuantize(PillowTestCase):
def test_sanity(self):
im = hopper()
image = hopper()
converted = image.quantize()
self.assert_image(converted, 'P', converted.size)
self.assert_image_similar(converted.convert('RGB'), image, 10)
im = im.quantize()
self.assert_image(im, "P", im.size)
im = hopper()
im = im.quantize(palette=hopper("P"))
self.assert_image(im, "P", im.size)
image = hopper()
converted = image.quantize(palette=hopper('P'))
self.assert_image(converted, 'P', converted.size)
self.assert_image_similar(converted.convert('RGB'), image, 60)
def test_libimagequant_quantize(self):
im = hopper()
image = hopper()
try:
im = im.quantize(100, Image.LIBIMAGEQUANT)
converted = image.quantize(100, Image.LIBIMAGEQUANT)
except ValueError as ex:
if 'dependency' in str(ex).lower():
self.skipTest('libimagequant support not available')
else:
raise
self.assert_image(im, "P", im.size)
assert len(im.getcolors()) == 100
self.assert_image(converted, 'P', converted.size)
self.assert_image_similar(converted.convert('RGB'), image, 15)
assert len(converted.getcolors()) == 100
def test_octree_quantize(self):
im = hopper()
im = im.quantize(100, Image.FASTOCTREE)
self.assert_image(im, "P", im.size)
assert len(im.getcolors()) == 100
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
def test_rgba_quantize(self):
im = hopper('RGBA')
im.quantize()
self.assertRaises(Exception, lambda: im.quantize(method=0))
image = hopper('RGBA')
image.quantize()
self.assertRaises(Exception, lambda: image.quantize(method=0))
def test_quantize(self):
im = Image.open('Tests/images/caption_6_33_22.png')
im.convert('RGB').quantize().convert('RGB')
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)
if __name__ == '__main__':