Skip to content

Commit

Permalink
Allow saving L mode TIFF with PhotometricInterpretation 0
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 6, 2021
1 parent 2dab52b commit 9bf7dae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Tests/test_file_tiff.py
Expand Up @@ -447,9 +447,10 @@ def test_exif_frames(self):
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
Expand Down
19 changes: 11 additions & 8 deletions src/PIL/TiffImagePlugin.py
Expand Up @@ -48,7 +48,7 @@
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

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 9bf7dae

Please sign in to comment.