Trim id_section if it is greater than 255 characters

This commit is contained in:
Andrew Murray 2018-09-08 08:26:32 +10:00
parent 9e4c54e10f
commit 325ca3cede
2 changed files with 13 additions and 0 deletions

View File

@ -124,6 +124,13 @@ class TestFileTga(PillowTestCase):
test_im = Image.open(out) test_im = Image.open(out)
self.assertEqual(test_im.info["id_section"], b"Test content") self.assertEqual(test_im.info["id_section"], b"Test content")
# Save with custom id section greater than 255 characters
id_section = b"Test content" * 25
self.assert_warning(UserWarning,
lambda: im.save(out, id_section=id_section))
test_im = Image.open(out)
self.assertEqual(test_im.info["id_section"], id_section[:255])
test_file = "Tests/images/tga_id_field.tga" test_file = "Tests/images/tga_id_field.tga"
im = Image.open(test_file) im = Image.open(test_file)

View File

@ -20,6 +20,8 @@
from . import Image, ImageFile, ImagePalette from . import Image, ImageFile, ImagePalette
from ._binary import i8, i16le as i16, o8, o16le as o16 from ._binary import i8, i16le as i16, o8, o16le as o16
import warnings
__version__ = "0.3" __version__ = "0.3"
@ -163,6 +165,10 @@ def _save(im, fp, filename):
id_section = im.encoderinfo.get("id_section", id_section = im.encoderinfo.get("id_section",
im.info.get("id_section", "")) im.info.get("id_section", ""))
idlen = len(id_section) idlen = len(id_section)
if idlen > 255:
idlen = 255
id_section = id_section[:255]
warnings.warn("id_section has been trimmed to 255 characters")
if colormaptype: if colormaptype:
colormapfirst, colormaplength, colormapentry = 0, 256, 24 colormapfirst, colormaplength, colormapentry = 0, 256, 24