diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index a475c55b9..d5dda5799 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -447,9 +447,10 @@ class TestFileTiff: im.seek(1) assert im.getexif()[273] == (1408, 1907) - def test_photometric(self, tmp_path): + @pytest.mark.parametrize("mode", ("1", "L")) + def test_photometric(self, mode, tmp_path): filename = str(tmp_path / "temp.tif") - im = hopper("1") + im = hopper(mode) im.save(filename, tiffinfo={262: 0}) with Image.open(filename) as reloaded: assert reloaded.tag_v2[262] == 0 diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 2858694f8..15678948c 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -48,7 +48,7 @@ from collections.abc import MutableMapping from fractions import Fraction from numbers import Number, Rational -from . import Image, ImageFile, ImagePalette, TiffTags +from . import Image, ImageFile, ImageOps, ImagePalette, TiffTags from ._binary import o8 from .TiffTags import TYPES @@ -1572,13 +1572,16 @@ def _save(im, fp, filename): if PHOTOMETRIC_INTERPRETATION not in ifd: ifd[PHOTOMETRIC_INTERPRETATION] = photo - elif im.mode == "1" and ifd[PHOTOMETRIC_INTERPRETATION] == 0: - inverted_im = im.copy() - px = inverted_im.load() - for y in range(inverted_im.height): - for x in range(inverted_im.width): - px[x, y] = 0 if px[x, y] == 255 else 255 - im = inverted_im + elif im.mode in ("1", "L") and ifd[PHOTOMETRIC_INTERPRETATION] == 0: + if im.mode == "1": + inverted_im = im.copy() + px = inverted_im.load() + for y in range(inverted_im.height): + for x in range(inverted_im.width): + px[x, y] = 0 if px[x, y] == 255 else 255 + im = inverted_im + else: + im = ImageOps.invert(im) if im.mode in ["P", "PA"]: lut = im.im.getpalette("RGB", "RGB;L")