Added support for top right and bottom right orientations

This commit is contained in:
Andrew Murray 2021-11-11 21:53:28 +11:00
parent 24f0bbf5ec
commit fc90a9285a
4 changed files with 16 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View File

@ -171,6 +171,15 @@ def test_save_orientation(tmp_path):
assert test_im.info["orientation"] == 1
def test_horizontal_orientations():
# These images have been manually hexedited to have the relevant orientations
with Image.open("Tests/images/rgb32rle_top_right.tga") as im:
assert im.load()[90, 90][:3] == (0, 0, 0)
with Image.open("Tests/images/rgb32rle_bottom_right.tga") as im:
assert im.load()[90, 90][:3] == (0, 255, 0)
def test_save_rle(tmp_path):
test_file = "Tests/images/rgb32rle.tga"
with Image.open(test_file) as im:

View File

@ -93,9 +93,10 @@ class TgaImageFile(ImageFile.ImageFile):
# orientation
orientation = flags & 0x30
if orientation == 0x20:
self._flip_horizontally = orientation in [0x10, 0x30]
if orientation in [0x20, 0x30]:
orientation = 1
elif not orientation:
elif orientation in [0, 0x10]:
orientation = -1
else:
raise SyntaxError("unknown TGA orientation")
@ -149,6 +150,10 @@ class TgaImageFile(ImageFile.ImageFile):
except KeyError:
pass # cannot decode
def load_end(self):
if self._flip_horizontally:
self.im = self.im.transpose(Image.FLIP_LEFT_RIGHT)
#
# --------------------------------------------------------------------