Skip to content

Commit

Permalink
Merge pull request #5829 from radarhere/tga_orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Nov 21, 2021
2 parents 877d97e + fc90a92 commit c7a9055
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Binary file added Tests/images/rgb32rle_bottom_right.tga
Binary file not shown.
Binary file added Tests/images/rgb32rle_top_right.tga
Binary file not shown.
9 changes: 9 additions & 0 deletions Tests/test_file_tga.py
Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions src/PIL/TgaImagePlugin.py
Expand Up @@ -93,9 +93,10 @@ def _open(self):

# 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")
Expand Down Expand Up @@ -149,6 +150,10 @@ def _open(self):
except KeyError:
pass # cannot decode

def load_end(self):
if self._flip_horizontally:
self.im = self.im.transpose(Image.FLIP_LEFT_RIGHT)


#
# --------------------------------------------------------------------
Expand Down

0 comments on commit c7a9055

Please sign in to comment.