Merge pull request #6232 from radarhere/colormap

This commit is contained in:
Hugo van Kemenade 2022-06-01 23:12:13 +03:00 committed by GitHub
commit 55d3c07e1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -498,8 +498,8 @@ class TestFileLibTiff(LibTiffTestCase):
im.save(out, compression="tiff_adobe_deflate")
assert_image_equal_tofile(im, out)
def test_palette_save(self, tmp_path):
im = hopper("P")
@pytest.mark.parametrize("im", (hopper("P"), Image.new("P", (1, 1), "#000")))
def test_palette_save(self, im, tmp_path):
out = str(tmp_path / "temp.tif")
TiffImagePlugin.WRITE_LIBTIFF = True

View File

@ -1674,7 +1674,12 @@ def _save(im, fp, filename):
if im.mode in ["P", "PA"]:
lut = im.im.getpalette("RGB", "RGB;L")
ifd[COLORMAP] = tuple(v * 256 for v in lut)
colormap = []
colors = len(lut) // 3
for i in range(3):
colormap += [v * 256 for v in lut[colors * i : colors * (i + 1)]]
colormap += [0] * (256 - colors)
ifd[COLORMAP] = colormap
# data orientation
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
# aim for given strip size (64 KB by default) when using libtiff writer