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

Corrected BMP and TGA palette size when saving #6500

Merged
merged 3 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions Tests/test_file_tga.py
Expand Up @@ -123,6 +123,18 @@ def test_save(tmp_path):
assert test_im.size == (100, 100)


def test_small_palette(tmp_path):
im = Image.new("P", (1, 1))
colors = [0, 0, 0]
im.putpalette(colors)

out = str(tmp_path / "temp.tga")
im.save(out)

with Image.open(out) as reloaded:
assert reloaded.getpalette() == colors


def test_save_wrong_mode(tmp_path):
im = hopper("PA")
out = str(tmp_path / "temp.tga")
Expand Down
9 changes: 5 additions & 4 deletions src/PIL/TgaImagePlugin.py
Expand Up @@ -193,9 +193,10 @@ def _save(im, fp, filename):
warnings.warn("id_section has been trimmed to 255 characters")

if colormaptype:
colormapfirst, colormaplength, colormapentry = 0, 256, 24
palette = im.im.getpalette("RGB", "BGR")
colormaplength, colormapentry = len(palette) // 3, 24
else:
colormapfirst, colormaplength, colormapentry = 0, 0, 0
colormaplength, colormapentry = 0, 0

if im.mode in ("LA", "RGBA"):
flags = 8
Expand All @@ -210,7 +211,7 @@ def _save(im, fp, filename):
o8(id_len)
+ o8(colormaptype)
+ o8(imagetype)
+ o16(colormapfirst)
+ o16(0) # colormapfirst
+ o16(colormaplength)
+ o8(colormapentry)
+ o16(0)
Expand All @@ -225,7 +226,7 @@ def _save(im, fp, filename):
fp.write(id_section)

if colormaptype:
fp.write(im.im.getpalette("RGB", "BGR"))
fp.write(palette)

if rle:
ImageFile._save(
Expand Down