Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jun 1, 2022

Conversation

radarhere
Copy link
Member

Resolves #6334

The issue features a user calling im.getexif(), modifying the exif data, and then saving to a TIFF file.

However, when saving TIFF makes sure that the exif data has been read with getexif(), which then loads the data with Exif.load_from_fp. This reloads the exif data, overriding the user's changes.

The solution is to prevent the exif data from being loaded again if it has already been parsed.

While Exif.load() checks that it is not reloading the same input, this is not done for Exif.load_from_fp(). To add that check specifically for load_from_fp would require storing the file pointer to ensure that it is the same file being read, and storing the file pointer would mean that it needs to be considered as part of the file handling lifecycle.

Instead, this PR adds a _loaded attribute. Once set, getexif() does not load the exif data anymore, but just returns the existing object. The attribute is cleared when the exif data is changed by seek operations so that the updated exif data can be loaded.

@radarhere radarhere added the Exif label May 26, 2022
@radarhere radarhere changed the title Once Exif data is parsed, do not reload unless it changes Once exif data is parsed, do not reload unless it changes May 26, 2022
@gverm
Copy link

gverm commented May 31, 2022

@radarhere Just tested this PR and it also fixes the issue.

@hugovk hugovk merged commit b261e17 into python-pillow:main Jun 1, 2022
@radarhere radarhere deleted the exif branch June 1, 2022 21:52
@bigcat88
Copy link
Contributor

bigcat88 commented Jul 2, 2022

@radarhere in my plugin pillow-heif i got a problem with new code.
there are separate exif info for each frame in HEIF, and with this changes, when changing to next frame exif thinks that it is already loaded.

        if self._exif is None:
            self._exif = Exif()
            self._exif._loaded = False
        elif self._exif._loaded:               # <--- this is always true when use ImageSequence.Iterator in Pillow 9.2
            return self._exif
        self._exif._loaded = True

should I reset in plugin self._exif._loaded this value when changing frame throw seek operation?

@radarhere
Copy link
Member Author

Hi. You could set self._exif._loaded = False in the plugin, sure.

Without knowing exactly what your code does, I might alternatively suggest calling self._reload_exif(), so that if the user has previously called getexif(), that object will be updated at the same time.

bigcat88 added a commit to bigcat88/pillow_heif that referenced this pull request Jul 2, 2022
gasman added a commit to demozoo/demozoo that referenced this pull request Nov 27, 2022
This fixes the issue with retrieving EXIF data from a TIFF file (probably in python-pillow/Pillow#6335), so the check for JPEG format is no longer needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TIFF EXIF data is not updated when saving image
4 participants