Pillow/Tests/test_imagepalette.py

154 lines
3.8 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()
2015-04-24 11:24:52 +03:00
test_map = {}
2014-06-10 13:10:47 +04:00
for i in range(256):
2015-04-24 11:24:52 +03:00
test_map[palette.getcolor((i, i, i))] = i
2015-04-24 11:24:52 +03:00
self.assertEqual(len(test_map), 256)
2014-06-10 13:10:47 +04:00
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
def test_make_linear_lut(self):
# Arrange
from PIL.ImagePalette import make_linear_lut
black = 0
white = 255
# Act
lut = make_linear_lut(black, white)
# Assert
self.assertIsInstance(lut, list)
self.assertEqual(len(lut), 256)
# Check values
for i in range(0, len(lut)):
self.assertEqual(lut[i], i)
def test_make_linear_lut_not_yet_implemented(self):
# Update after FIXME
# Arrange
from PIL.ImagePalette import make_linear_lut
black = 1
white = 255
# Act
self.assertRaises(
NotImplementedError,
lambda: make_linear_lut(black, white))
2014-07-19 02:45:57 +04:00
def test_make_gamma_lut(self):
# Arrange
from PIL.ImagePalette import make_gamma_lut
exp = 5
# Act
lut = make_gamma_lut(exp)
# Assert
self.assertIsInstance(lut, list)
self.assertEqual(len(lut), 256)
# Check a few values
self.assertEqual(lut[0], 0)
self.assertEqual(lut[63], 0)
self.assertEqual(lut[127], 8)
self.assertEqual(lut[191], 60)
self.assertEqual(lut[255], 255)
def test_private_make_linear_lut_warning(self):
# Arrange
from PIL.ImagePalette import _make_linear_lut
black = 0
white = 255
# Act / Assert
self.assert_warning(
DeprecationWarning,
lambda: _make_linear_lut(black, white))
def test_private_make_gamma_lut_warning(self):
# Arrange
from PIL.ImagePalette import _make_gamma_lut
exp = 5
# Act / Assert
self.assert_warning(
DeprecationWarning,
lambda: _make_gamma_lut(exp))
def test_rawmode_valueerrors(self):
# Arrange
from PIL.ImagePalette import raw
palette = raw("RGB", list(range(256))*3)
# Act / Assert
2015-04-02 10:08:14 +03:00
self.assertRaises(ValueError, palette.tobytes)
self.assertRaises(ValueError, lambda: palette.getcolor((1, 2, 3)))
f = self.tempfile("temp.lut")
self.assertRaises(ValueError, lambda: palette.save(f))
def test_getdata(self):
# Arrange
data_in = list(range(256))*3
palette = ImagePalette("RGB", data_in)
# Act
mode, data_out = palette.getdata()
# Assert
self.assertEqual(mode, "RGB;L")
def test_rawmode_getdata(self):
# Arrange
from PIL.ImagePalette import raw
data_in = list(range(256))*3
palette = raw("RGB", data_in)
# Act
rawmode, data_out = palette.getdata()
# Assert
self.assertEqual(rawmode, "RGB")
self.assertEqual(data_in, data_out)
2014-07-19 02:45:57 +04:00
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()
# End of file