Fixed processing multiple JPEG EXIF markers by radarhere · Pull Request #8127 · python-pillow/Pillow
Resolves #8116
#7496 added concatenating of multiple JPEG EXIF markers.
However, the resulting image may not have the correct data in getexif(), as getexif() might be called after reading the first EXIF marker in attempt to read the DPI
| if "dpi" not in self.info and "exif" in self.info: | |
| try: | |
| exif = self.getexif() |
and getexif() is cached from that point on.
| def getexif(self) -> Exif: | |
| """ | |
| Gets EXIF data from the image. | |
| :returns: an :py:class:`~PIL.Image.Exif` object. | |
| """ | |
| if self._exif is None: | |
| self._exif = Exif() | |
| elif self._exif._loaded: | |
| return self._exif |
So this PR moves the DPI reading until after all markers are processed.
I've updated the test image from #7496, using the following code.
>>> from PIL import Image >>> exif = Image.Exif() >>> exif[270] = "firstsecond" >>> exif.tobytes() b'Exif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x01\x01\x0e\x00\x02\x00\x00\x00\x0c\x00\x00\x00\x1a\x00\x00\x00\x00firstsecond\x00'
im = Image.new("L", (1, 1)) im.save("Tests/images/multiple_exif.jpg", exif=b'Exif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x01\x01\x0e\x00\x02\x00\x00\x00\x0c\x00\x00\x00\x1a\x00\x00\x00\x00first', extra=b'\xFF\xE1\x00\x0FExif\x00\x00second\x00')