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

Prevent TIFF orientation from being applied more than once #7383

Merged
merged 4 commits into from
Sep 11, 2023

Conversation

radarhere
Copy link
Member

Resolves #7381

When a TIFF image is loaded, the orientation is applied.

self._tile_orientation = self.tag_v2.get(ExifTags.Base.Orientation)

def load_end(self):
if self._tile_orientation:
method = {
2: Image.Transpose.FLIP_LEFT_RIGHT,
3: Image.Transpose.ROTATE_180,
4: Image.Transpose.FLIP_TOP_BOTTOM,
5: Image.Transpose.TRANSPOSE,
6: Image.Transpose.ROTATE_270,
7: Image.Transpose.TRANSVERSE,
8: Image.Transpose.ROTATE_90,
}.get(self._tile_orientation)
if method is not None:
self.im = self.im.transpose(method)
self._size = self.im.size

However, there is no change in Pillow's information about the image as a result of this. This is not ideal, because

  1. if a user inspects the tag_v2 orientation for an image, it may or may not have been applied already, depending on whether the image has loaded, and that could be confusing.
  2. If a user runs ImageOps.exif_transpose() afterwards, the orientation is applied a second time.

This PR addresses 1 by deleting the tag_v2 orientation tag, so that it is consistently present if not applied yet, and removed once it has been applied. It address 2 by calling ImageOps.exif_transpose(self, in_place=True) from TiffImagePlugin, which also reduces duplicate code.

The issue also calls exif_transpose() immediately after opening an image, without load(). So it is necessary to call load() the image in exif_transpose(), so that the image's orientation is not initially detected by exif_transpose, but then quickly changed once the image is loaded to perform a transposition.

@radarhere radarhere changed the title Only apply TIFF orientation once Prevent TIFF orientation from being applied more than once Sep 7, 2023
src/PIL/ImageOps.py Outdated Show resolved Hide resolved
@hugovk hugovk merged commit d06e939 into python-pillow:main Sep 11, 2023
48 of 50 checks passed
@radarhere radarhere deleted the tiff_orientation branch September 11, 2023 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ImageOps.exif_transpose not expected behaviour for TIFF images
3 participants