Pillow/Tests/test_image_quantize.py

66 lines
2.4 KiB
Python
Raw Normal View History

from .helper import PillowTestCase, hopper
from PIL import Image
2014-06-10 13:10:47 +04:00
class TestImageQuantize(PillowTestCase):
2014-06-10 13:10:47 +04:00
def test_sanity(self):
image = hopper()
converted = image.quantize()
self.assert_image(converted, 'P', converted.size)
self.assert_image_similar(converted.convert('RGB'), image, 10)
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):
image = hopper()
try:
converted = image.quantize(100, Image.LIBIMAGEQUANT)
except ValueError as ex:
if 'dependency' in str(ex).lower():
2016-05-07 00:29:53 +03:00
self.skipTest('libimagequant support not available')
else:
raise
self.assert_image(converted, 'P', converted.size)
self.assert_image_similar(converted.convert('RGB'), image, 15)
self.assertEqual(len(converted.getcolors()), 100)
2014-06-10 13:10:47 +04:00
def test_octree_quantize(self):
image = hopper()
converted = image.quantize(100, Image.FASTOCTREE)
self.assert_image(converted, 'P', converted.size)
self.assert_image_similar(converted.convert('RGB'), image, 20)
self.assertEqual(len(converted.getcolors()), 100)
2014-06-10 13:10:47 +04:00
def test_rgba_quantize(self):
image = hopper('RGBA')
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):
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)
def test_quantize_no_dither(self):
image = hopper()
palette = Image.open('Tests/images/caption_6_33_22.png').convert('P')
converted = image.quantize(dither=0, palette=palette)
self.assert_image(converted, 'P', converted.size)
def test_quantize_dither_diff(self):
image = hopper()
palette = Image.open('Tests/images/caption_6_33_22.png').convert('P')
dither = image.quantize(dither=1, palette=palette)
nodither = image.quantize(dither=0, palette=palette)
self.assertNotEqual(dither.tobytes(), nodither.tobytes())