Skip to content

Commit 1b70a4c

Browse files
committed
Use TIFF orientation
1 parent 929c817 commit 1b70a4c

10 files changed

+28
-0
lines changed

Tests/images/g4_orientation_1.tif

708 Bytes
Binary file not shown.

Tests/images/g4_orientation_2.tif

930 Bytes
Binary file not shown.

Tests/images/g4_orientation_3.tif

926 Bytes
Binary file not shown.

Tests/images/g4_orientation_4.tif

928 Bytes
Binary file not shown.

Tests/images/g4_orientation_5.tif

1 KB
Binary file not shown.

Tests/images/g4_orientation_6.tif

1 KB
Binary file not shown.

Tests/images/g4_orientation_7.tif

1022 Bytes
Binary file not shown.

Tests/images/g4_orientation_8.tif

1 KB
Binary file not shown.

Tests/test_file_libtiff.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,12 @@ def test_old_style_jpeg(self):
831831
self.assert_image_equal_tofile(
832832
im, "Tests/images/old-style-jpeg-compression.png"
833833
)
834+
835+
def test_orientation(self):
836+
base_im = Image.open("Tests/images/g4_orientation_1.tif")
837+
838+
for i in range(2, 9):
839+
im = Image.open("Tests/images/g4_orientation_" + str(i) + ".tif")
840+
im.load()
841+
842+
self.assert_image_similar(base_im, im, 0.7)

src/PIL/TiffImagePlugin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,20 @@ def load(self):
10981098
return super(TiffImageFile, self).load()
10991099

11001100
def load_end(self):
1101+
if self._tile_orientation:
1102+
method = {
1103+
2: Image.FLIP_LEFT_RIGHT,
1104+
3: Image.ROTATE_180,
1105+
4: Image.FLIP_TOP_BOTTOM,
1106+
5: Image.TRANSPOSE,
1107+
6: Image.ROTATE_270,
1108+
7: Image.TRANSVERSE,
1109+
8: Image.ROTATE_90,
1110+
}.get(self._tile_orientation)
1111+
if method is not None:
1112+
self.im = self.im.transpose(method)
1113+
self._size = self.im.size
1114+
11011115
# allow closing if we're on the first frame, there's no next
11021116
# This is the ImageFile.load path only, libtiff specific below.
11031117
if not self._is_animated:
@@ -1181,6 +1195,9 @@ def _load_libtiff(self):
11811195

11821196
self.tile = []
11831197
self.readonly = 0
1198+
1199+
self.load_end()
1200+
11841201
# libtiff closed the fp in a, we need to close self.fp, if possible
11851202
if self._exclusive_fp and not self._is_animated:
11861203
self.fp.close()
@@ -1388,6 +1405,8 @@ def _setup(self):
13881405
palette = [o8(b // 256) for b in self.tag_v2[COLORMAP]]
13891406
self.palette = ImagePalette.raw("RGB;L", b"".join(palette))
13901407

1408+
self._tile_orientation = self.tag_v2.get(0x0112)
1409+
13911410
def _close__fp(self):
13921411
try:
13931412
if self.__fp != self.fp:

0 commit comments

Comments
 (0)