2014-07-07 21:03:50 +04:00
|
|
|
from helper import unittest, PillowTestCase
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import ImagePalette
|
|
|
|
|
|
|
|
ImagePalette = ImagePalette.ImagePalette
|
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImagePalette(PillowTestCase):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
ImagePalette("RGB", list(range(256))*3)
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError, lambda: ImagePalette("RGB", list(range(256))*2))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_getcolor(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
palette = ImagePalette()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
map = {}
|
|
|
|
for i in range(256):
|
|
|
|
map[palette.getcolor((i, i, i))] = i
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(len(map), 256)
|
|
|
|
self.assertRaises(ValueError, lambda: palette.getcolor((1, 2, 3)))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_file(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-24 20:58:53 +04:00
|
|
|
palette = ImagePalette("RGB", list(range(256))*3)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-24 20:58:53 +04:00
|
|
|
f = self.tempfile("temp.lut")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-24 20:58:53 +04:00
|
|
|
palette.save(f)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
from PIL.ImagePalette import load, raw
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-24 20:58:53 +04:00
|
|
|
p = load(f)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# load returns raw palette information
|
|
|
|
self.assertEqual(len(p[0]), 768)
|
|
|
|
self.assertEqual(p[1], "RGB")
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
p = raw(p[1], p[0])
|
|
|
|
self.assertIsInstance(p, ImagePalette)
|
2014-06-24 20:58:53 +04:00
|
|
|
self.assertEqual(p.palette, palette.tobytes())
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# End of file
|