Once exif data is parsed, do not reload unless it changes

This commit is contained in:
Andrew Murray 2022-05-27 07:54:54 +10:00
parent c34d78934f
commit b1885779a9
5 changed files with 42 additions and 0 deletions

View File

@ -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:

View File

@ -497,6 +497,26 @@ class TestFileTiff:
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:

View File

@ -1383,6 +1383,10 @@ class Image:
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:
@ -1407,6 +1411,12 @@ class Image:
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.

View File

@ -82,6 +82,7 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
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"):
@ -90,6 +91,7 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
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

View File

@ -1136,6 +1136,7 @@ class TiffImageFile(ImageFile.ImageFile):
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