Pillow/Tests/test_image_getcolors.py

75 lines
2.1 KiB
Python
Raw Normal View History

2014-07-07 21:03:50 +04:00
from helper import unittest, PillowTestCase, lena
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 = lena(mode)
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"), 193)
self.assertEqual(getcolors("I"), 193)
self.assertEqual(getcolors("F"), 193)
self.assertEqual(getcolors("P"), 54) # fixed palette
self.assertEqual(getcolors("RGB"), None)
self.assertEqual(getcolors("RGBA"), None)
self.assertEqual(getcolors("CMYK"), None)
self.assertEqual(getcolors("YCbCr"), None)
2014-06-10 13:10:47 +04:00
self.assertEqual(getcolors("L", 128), None)
self.assertEqual(getcolors("L", 1024), 193)
2014-06-10 13:10:47 +04:00
self.assertEqual(getcolors("RGB", 8192), None)
self.assertEqual(getcolors("RGB", 16384), 14836)
self.assertEqual(getcolors("RGB", 100000), 14836)
2014-06-10 13:10:47 +04:00
self.assertEqual(getcolors("RGBA", 16384), 14836)
self.assertEqual(getcolors("CMYK", 16384), 14836)
self.assertEqual(getcolors("YCbCr", 16384), 11995)
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)
2014-06-10 13:10:47 +04:00
im = lena().quantize(3).convert("RGB")
2014-06-10 13:10:47 +04:00
expected = [
(3236, (227, 183, 147)),
(6297, (143, 84, 81)),
(6851, (208, 143, 112))]
2014-06-10 13:10:47 +04:00
A = im.getcolors(maxcolors=2)
self.assertEqual(A, None)
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)
if __name__ == '__main__':
unittest.main()
# End of file