Skip to content

Commit

Permalink
Once exif data is parsed, do not reload unless it changes
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 26, 2022
1 parent c34d789 commit b188577
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Tests/test_file_mpo.py
Expand Up @@ -124,6 +124,15 @@ def test_parallax():
assert exif.get_ifd(0x927C)[0xB211] == -3.125


def test_reload_exif_after_seek():
with Image.open("Tests/images/sugarshack.mpo") as im:
exif = im.getexif()
del exif[296]

im.seek(1)
assert 296 in exif


def test_mp():
for test_file in test_files:
with Image.open(test_file) as im:
Expand Down
20 changes: 20 additions & 0 deletions Tests/test_file_tiff.py
Expand Up @@ -497,6 +497,26 @@ def check_exif(exif):
exif = im.getexif()
check_exif(exif)

def test_modify_exif(self, tmp_path):
outfile = str(tmp_path / "temp.tif")
with Image.open("Tests/images/ifd_tag_type.tiff") as im:
exif = im.getexif()
exif[256] = 100

im.save(outfile, exif=exif)

with Image.open(outfile) as im:
exif = im.getexif()
assert exif[256] == 100

def test_reload_exif_after_seek(self):
with Image.open("Tests/images/multipage.tiff") as im:
exif = im.getexif()
del exif[256]
im.seek(1)

assert 256 in exif

def test_exif_frames(self):
# Test that EXIF data can change across frames
with Image.open("Tests/images/g4-multi.tiff") as im:
Expand Down
10 changes: 10 additions & 0 deletions src/PIL/Image.py
Expand Up @@ -1383,6 +1383,10 @@ def get_value(element):
def getexif(self):
if self._exif is None:
self._exif = Exif()
self._exif._loaded = False
elif self._exif._loaded:
return self._exif
self._exif._loaded = True

exif_info = self.info.get("exif")
if exif_info is None:
Expand All @@ -1407,6 +1411,12 @@ def getexif(self):

return self._exif

def _reload_exif(self):
if self._exif is None or not self._exif._loaded:
return
self._exif._loaded = False
self.getexif()

def getim(self):
"""
Returns a capsule that points to the internal image memory.
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/MpoImagePlugin.py
Expand Up @@ -82,6 +82,7 @@ def seek(self, frame):
if i16(segment) == 0xFFE1: # APP1
n = i16(self.fp.read(2)) - 2
self.info["exif"] = ImageFile._safe_read(self.fp, n)
self._reload_exif()

mptype = self.mpinfo[0xB002][frame]["Attribute"]["MPType"]
if mptype.startswith("Large Thumbnail"):
Expand All @@ -90,6 +91,7 @@ def seek(self, frame):
self._size = (exif[40962], exif[40963])
elif "exif" in self.info:
del self.info["exif"]
self._reload_exif()

self.tile = [("jpeg", (0, 0) + self.size, self.offset, (self.mode, ""))]
self.__frame = frame
Expand Down
1 change: 1 addition & 0 deletions src/PIL/TiffImagePlugin.py
Expand Up @@ -1136,6 +1136,7 @@ def _seek(self, frame):
self.__frame += 1
self.fp.seek(self._frame_pos[frame])
self.tag_v2.load(self.fp)
self._reload_exif()
# fill the legacy tag/ifd entries
self.tag = self.ifd = ImageFileDirectory_v1.from_v2(self.tag_v2)
self.__frame = frame
Expand Down

0 comments on commit b188577

Please sign in to comment.