2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import Image, ImagePalette
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import PillowTestCase, assert_image_equal
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImagePalette(PillowTestCase):
|
|
|
|
def test_sanity(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
ImagePalette.ImagePalette("RGB", list(range(256)) * 3)
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ImagePalette.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
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
palette = ImagePalette.ImagePalette()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
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
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(test_map) == 256
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette.getcolor((1, 2, 3))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
# Test unknown color specifier
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette.getcolor("unknown")
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_file(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
palette = ImagePalette.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
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
p = ImagePalette.load(f)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# load returns raw palette information
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(p[0]) == 768
|
|
|
|
assert p[1] == "RGB"
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
p = ImagePalette.raw(p[1], p[0])
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(p, ImagePalette.ImagePalette)
|
|
|
|
assert p.palette == palette.tobytes()
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-07-20 11:13:26 +04:00
|
|
|
def test_make_linear_lut(self):
|
|
|
|
# Arrange
|
|
|
|
black = 0
|
|
|
|
white = 255
|
|
|
|
|
|
|
|
# Act
|
2017-09-01 13:36:51 +03:00
|
|
|
lut = ImagePalette.make_linear_lut(black, white)
|
2014-07-20 11:13:26 +04:00
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(lut, list)
|
|
|
|
assert len(lut) == 256
|
2014-07-20 11:13:26 +04:00
|
|
|
# Check values
|
|
|
|
for i in range(0, len(lut)):
|
2020-02-22 16:06:21 +03:00
|
|
|
assert lut[i] == i
|
2014-07-20 11:13:26 +04:00
|
|
|
|
|
|
|
def test_make_linear_lut_not_yet_implemented(self):
|
|
|
|
# Update after FIXME
|
|
|
|
# Arrange
|
|
|
|
black = 1
|
|
|
|
white = 255
|
|
|
|
|
|
|
|
# Act
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
ImagePalette.make_linear_lut(black, white)
|
2014-07-20 11:13:26 +04:00
|
|
|
|
2014-07-19 02:45:57 +04:00
|
|
|
def test_make_gamma_lut(self):
|
|
|
|
# Arrange
|
|
|
|
exp = 5
|
|
|
|
|
|
|
|
# Act
|
2017-09-01 13:36:51 +03:00
|
|
|
lut = ImagePalette.make_gamma_lut(exp)
|
2014-07-19 02:45:57 +04:00
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(lut, list)
|
|
|
|
assert len(lut) == 256
|
2014-07-19 02:45:57 +04:00
|
|
|
# Check a few values
|
2020-02-22 16:06:21 +03:00
|
|
|
assert lut[0] == 0
|
|
|
|
assert lut[63] == 0
|
|
|
|
assert lut[127] == 8
|
|
|
|
assert lut[191] == 60
|
|
|
|
assert lut[255] == 255
|
2014-07-19 02:45:57 +04:00
|
|
|
|
2014-07-22 00:18:46 +04:00
|
|
|
def test_rawmode_valueerrors(self):
|
|
|
|
# Arrange
|
2019-06-13 18:54:46 +03:00
|
|
|
palette = ImagePalette.raw("RGB", list(range(256)) * 3)
|
2014-07-22 00:18:46 +04:00
|
|
|
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette.tobytes()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette.getcolor((1, 2, 3))
|
2014-07-22 00:18:46 +04:00
|
|
|
f = self.tempfile("temp.lut")
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette.save(f)
|
2014-07-22 00:18:46 +04:00
|
|
|
|
|
|
|
def test_getdata(self):
|
|
|
|
# Arrange
|
2019-06-13 18:54:46 +03:00
|
|
|
data_in = list(range(256)) * 3
|
2017-09-01 13:36:51 +03:00
|
|
|
palette = ImagePalette.ImagePalette("RGB", data_in)
|
2014-07-22 00:18:46 +04:00
|
|
|
|
|
|
|
# Act
|
|
|
|
mode, data_out = palette.getdata()
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert mode == "RGB;L"
|
2014-07-22 00:18:46 +04:00
|
|
|
|
|
|
|
def test_rawmode_getdata(self):
|
|
|
|
# Arrange
|
2019-06-13 18:54:46 +03:00
|
|
|
data_in = list(range(256)) * 3
|
2017-09-01 13:36:51 +03:00
|
|
|
palette = ImagePalette.raw("RGB", data_in)
|
2014-07-22 00:18:46 +04:00
|
|
|
|
|
|
|
# Act
|
|
|
|
rawmode, data_out = palette.getdata()
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
assert rawmode == "RGB"
|
|
|
|
assert data_in == data_out
|
2014-07-22 00:18:46 +04:00
|
|
|
|
2016-12-03 17:08:59 +03:00
|
|
|
def test_2bit_palette(self):
|
|
|
|
# issue #2258, 2 bit palettes are corrupted.
|
2019-06-13 18:54:46 +03:00
|
|
|
outfile = self.tempfile("temp.png")
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
rgb = b"\x00" * 2 + b"\x01" * 2 + b"\x02" * 2
|
|
|
|
img = Image.frombytes("P", (6, 1), rgb)
|
|
|
|
img.putpalette(b"\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF") # RGB
|
|
|
|
img.save(outfile, format="PNG")
|
2016-12-03 17:08:59 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(outfile) as reloaded:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(img, reloaded)
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
def test_invalid_palette(self):
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(IOError):
|
|
|
|
ImagePalette.load("Tests/images/hopper.jpg")
|