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 00b1775
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 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
15 changes: 9 additions & 6 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,12 +1572,15 @@ def _save(im, fp, filename):

if PHOTOMETRIC_INTERPRETATION not in ifd:
ifd[PHOTOMETRIC_INTERPRETATION] = photo
elif im.mode == "1" and ifd[PHOTOMETRIC_INTERPRETATION] == 0:
elif im.mode in ("1", "L") 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
if im.mode == "1":
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
else:
inverted_im = ImageOps.invert(im)
im = inverted_im

if im.mode in ["P", "PA"]:
Expand Down

0 comments on commit 00b1775

Please sign in to comment.