2019-02-03 18:34: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 TestImageGetColors(PillowTestCase):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_getcolors(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def getcolors(mode, limit=None):
|
2014-09-05 14:03:56 +04:00
|
|
|
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
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(getcolors("1"), 2)
|
2014-09-14 21:00:13 +04:00
|
|
|
self.assertEqual(getcolors("L"), 255)
|
|
|
|
self.assertEqual(getcolors("I"), 255)
|
|
|
|
self.assertEqual(getcolors("F"), 255)
|
|
|
|
self.assertEqual(getcolors("P"), 90) # fixed palette
|
2017-06-03 07:04:54 +03:00
|
|
|
self.assertIsNone(getcolors("RGB"))
|
|
|
|
self.assertIsNone(getcolors("RGBA"))
|
|
|
|
self.assertIsNone(getcolors("CMYK"))
|
|
|
|
self.assertIsNone(getcolors("YCbCr"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2017-06-03 07:04:54 +03:00
|
|
|
self.assertIsNone(getcolors("L", 128))
|
2014-09-14 21:00:13 +04:00
|
|
|
self.assertEqual(getcolors("L", 1024), 255)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2017-06-03 07:04:54 +03:00
|
|
|
self.assertIsNone(getcolors("RGB", 8192))
|
2014-09-14 21:00:13 +04:00
|
|
|
self.assertEqual(getcolors("RGB", 16384), 10100)
|
|
|
|
self.assertEqual(getcolors("RGB", 100000), 10100)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-14 21:00:13 +04:00
|
|
|
self.assertEqual(getcolors("RGBA", 16384), 10100)
|
|
|
|
self.assertEqual(getcolors("CMYK", 16384), 10100)
|
|
|
|
self.assertEqual(getcolors("YCbCr", 16384), 9329)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# --------------------------------------------------------------------
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_pack(self):
|
|
|
|
# Pack problems for small tables (@PIL209)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
im = hopper().quantize(3).convert("RGB")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-14 21:00:13 +04:00
|
|
|
expected = [(4039, (172, 166, 181)),
|
|
|
|
(4385, (124, 113, 134)),
|
|
|
|
(7960, (31, 20, 33))]
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
A = im.getcolors(maxcolors=2)
|
2017-06-03 07:04:54 +03:00
|
|
|
self.assertIsNone(A)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
A = im.getcolors(maxcolors=3)
|
|
|
|
A.sort()
|
|
|
|
self.assertEqual(A, expected)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
A = im.getcolors(maxcolors=4)
|
|
|
|
A.sort()
|
|
|
|
self.assertEqual(A, expected)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
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)
|