Block tile TIFF tags when saving

This commit is contained in:
Andrew Murray 2021-11-18 22:01:53 +11:00
parent d6e42f3307
commit 7d4a8668b1
2 changed files with 24 additions and 1 deletions

View File

@ -920,6 +920,23 @@ class TestFileLibTiff(LibTiffTestCase):
with Image.open("Tests/images/tiff_strip_planar_16bit_RGBa.tiff") as im: with Image.open("Tests/images/tiff_strip_planar_16bit_RGBa.tiff") as im:
assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGBa_target.png") assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGBa_target.png")
@pytest.mark.parametrize("compression", (None, "jpeg"))
def test_block_tile_tags(self, compression, tmp_path):
im = hopper()
out = str(tmp_path / "temp.tif")
tags = {
TiffImagePlugin.TILEWIDTH: 256,
TiffImagePlugin.TILELENGTH: 256,
TiffImagePlugin.TILEOFFSETS: 256,
TiffImagePlugin.TILEBYTECOUNTS: 256,
}
im.save(out, exif=tags, compression=compression)
with Image.open(out) as reloaded:
for tag in tags.keys():
assert tag not in reloaded.getexif()
def test_old_style_jpeg(self): def test_old_style_jpeg(self):
with Image.open("Tests/images/old-style-jpeg-compression.tif") as im: with Image.open("Tests/images/old-style-jpeg-compression.tif") as im:
assert_image_equal_tofile(im, "Tests/images/old-style-jpeg-compression.png") assert_image_equal_tofile(im, "Tests/images/old-style-jpeg-compression.png")

View File

@ -89,7 +89,10 @@ DATE_TIME = 306
ARTIST = 315 ARTIST = 315
PREDICTOR = 317 PREDICTOR = 317
COLORMAP = 320 COLORMAP = 320
TILEWIDTH = 322
TILELENGTH = 323
TILEOFFSETS = 324 TILEOFFSETS = 324
TILEBYTECOUNTS = 325
SUBIFD = 330 SUBIFD = 330
EXTRASAMPLES = 338 EXTRASAMPLES = 338
SAMPLEFORMAT = 339 SAMPLEFORMAT = 339
@ -1649,6 +1652,7 @@ def _save(im, fp, filename):
}.items(): }.items():
ifd.setdefault(tag, value) ifd.setdefault(tag, value)
blocklist = [TILEWIDTH, TILELENGTH, TILEOFFSETS, TILEBYTECOUNTS]
if libtiff: if libtiff:
if "quality" in encoderinfo: if "quality" in encoderinfo:
quality = encoderinfo["quality"] quality = encoderinfo["quality"]
@ -1680,7 +1684,7 @@ def _save(im, fp, filename):
# BITSPERSAMPLE, etc), passing arrays with a different length will result in # BITSPERSAMPLE, etc), passing arrays with a different length will result in
# segfaults. Block these tags until we add extra validation. # segfaults. Block these tags until we add extra validation.
# SUBIFD may also cause a segfault. # SUBIFD may also cause a segfault.
blocklist = [ blocklist += [
REFERENCEBLACKWHITE, REFERENCEBLACKWHITE,
SAMPLEFORMAT, SAMPLEFORMAT,
STRIPBYTECOUNTS, STRIPBYTECOUNTS,
@ -1753,6 +1757,8 @@ def _save(im, fp, filename):
raise OSError(f"encoder error {s} when writing image file") raise OSError(f"encoder error {s} when writing image file")
else: else:
for tag in blocklist:
del ifd[tag]
offset = ifd.save(fp) offset = ifd.save(fp)
ImageFile._save( ImageFile._save(