Pillow/Tests/test_imagepalette.py

51 lines
1.1 KiB
Python
Raw Normal View History

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