Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pad COLORMAP to 768 items when saving TIFF #6232

Merged
merged 1 commit into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Tests/test_file_libtiff.py
Expand Up @@ -497,8 +497,8 @@ def test_cmyk_save(self, tmp_path):
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
Expand Down
7 changes: 6 additions & 1 deletion src/PIL/TiffImagePlugin.py
Expand Up @@ -1676,7 +1676,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
Expand Down