Pillow/Tests/test_image_getcolors.py

68 lines
2.0 KiB
Python
Raw Normal View History

from .helper import PillowTestCase, hopper
2014-06-10 13:10:47 +04:00
class TestImageGetColors(PillowTestCase):
2014-06-10 13:10:47 +04:00
def test_getcolors(self):
2014-06-10 13:10:47 +04:00
def getcolors(mode, limit=None):
im = hopper(mode)
2014-06-10 13:10:47 +04:00
if limit:
colors = im.getcolors(limit)
else:
colors = im.getcolors()
if colors:
return len(colors)
return None
2014-06-10 13:10:47 +04:00
self.assertEqual(getcolors("1"), 2)
self.assertEqual(getcolors("L"), 255)
self.assertEqual(getcolors("F"), 255)
self.assertEqual(getcolors("P"), 90) # fixed palette
self.assertIsNone(getcolors("RGB"))
self.assertIsNone(getcolors("RGBA"))
self.assertIsNone(getcolors("CMYK"))
self.assertIsNone(getcolors("YCbCr"))
2019-05-08 14:58:33 +03:00
self.assertIsNone(getcolors("I"))
self.assertIsNone(getcolors("L", 128))
self.assertEqual(getcolors("L", 1024), 255)
self.assertIsNone(getcolors("RGB", 8192))
self.assertEqual(getcolors("RGB", 16384), 10100)
self.assertEqual(getcolors("RGB", 100000), 10100)
self.assertEqual(getcolors("RGBA", 16384), 10100)
self.assertEqual(getcolors("CMYK", 16384), 10100)
self.assertEqual(getcolors("YCbCr", 16384), 9329)
2014-06-10 13:10:47 +04:00
# --------------------------------------------------------------------
2014-06-10 13:10:47 +04:00
def test_pack(self):
# Pack problems for small tables (@PIL209)
im = hopper().quantize(3).convert("RGB")
expected = [(4039, (172, 166, 181)),
(4385, (124, 113, 134)),
(7960, (31, 20, 33))]
2014-06-10 13:10:47 +04:00
A = im.getcolors(maxcolors=2)
self.assertIsNone(A)
2014-06-10 13:10:47 +04:00
A = im.getcolors(maxcolors=3)
A.sort()
self.assertEqual(A, expected)
2014-06-10 13:10:47 +04:00
A = im.getcolors(maxcolors=4)
A.sort()
self.assertEqual(A, expected)
2014-06-10 13:10:47 +04:00
A = im.getcolors(maxcolors=8)
A.sort()
self.assertEqual(A, expected)
A = im.getcolors(maxcolors=16)
A.sort()
self.assertEqual(A, expected)