Skip to content

Commit

Permalink
TGA: Read and write LA data
Browse files Browse the repository at this point in the history
  • Loading branch information
danpla committed Jun 14, 2018
1 parent 956432e commit 39fae6e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Binary file added Tests/images/la.tga
Binary file not shown.
24 changes: 19 additions & 5 deletions Tests/test_file_tga.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ def test_save(self):
test_im = Image.open(test_file)
self.assertEqual(test_im.size, (100, 100))

# Unsupported mode save
self.assertRaises(IOError, lambda: im.convert("LA").save(test_file))

def test_save_rle(self):
test_file = "Tests/images/rgb32rle.tga"
im = Image.open(test_file)
Expand All @@ -60,8 +57,25 @@ def test_save_rle(self):
test_im = Image.open(test_file)
self.assertEqual(test_im.size, (199, 199))

# Unsupported mode save
self.assertRaises(IOError, lambda: im.convert("LA").save(test_file))
def test_save_l_transparency(self):
# There are 559 transparent pixels in la.tga.
num_transparent = 559

in_file = "Tests/images/la.tga"
im = Image.open(in_file)
self.assertEqual(im.mode, "LA")
self.assertEqual(
im.getchannel("A").getcolors()[0][0], num_transparent)

test_file = self.tempfile("temp.tga")
im.save(test_file)

test_im = Image.open(test_file)
self.assertEqual(test_im.mode, "LA")
self.assertEqual(
test_im.getchannel("A").getcolors()[0][0], num_transparent)

self.assert_image_equal(im, test_im)


if __name__ == '__main__':
Expand Down
12 changes: 7 additions & 5 deletions docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,13 @@ For more information about the SPIDER image processing package, see the
.. _SPIDER homepage: https://spider.wadsworth.org/spider_doc/spider/docs/spider.html
.. _Wadsworth Center: https://www.wadsworth.org/

TGA
^^^

PIL reads and writes TGA images containing ``L``, ``LA``, ``P``,
``RGB``, and ``RGBA`` data. PIL can read both uncompressed and
run-length encoded TGAs, but writes only uncompressed data.

TIFF
^^^^

Expand Down Expand Up @@ -927,11 +934,6 @@ PSD
PIL identifies and reads PSD files written by Adobe Photoshop 2.5 and 3.0.


TGA
^^^

PIL reads 24- and 32-bit uncompressed and run-length encoded TGA files.

WAL
^^^

Expand Down
6 changes: 5 additions & 1 deletion src/PIL/TgaImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
(1, 8): "P",
(3, 1): "1",
(3, 8): "L",
(3, 16): "LA",
(2, 16): "BGR;5",
(2, 24): "BGR",
(2, 32): "BGRA",
Expand Down Expand Up @@ -74,6 +75,8 @@ def _open(self):
self.mode = "L"
if depth == 1:
self.mode = "1" # ???
elif depth == 16:
self.mode = "LA"
elif imagetype in (1, 9):
self.mode = "P"
elif imagetype in (2, 10):
Expand Down Expand Up @@ -134,6 +137,7 @@ def _open(self):
SAVE = {
"1": ("1", 1, 0, 3),
"L": ("L", 8, 0, 3),
"LA": ("LA", 16, 0, 3),
"P": ("P", 8, 1, 1),
"RGB": ("BGR", 24, 0, 2),
"RGBA": ("BGRA", 32, 0, 2),
Expand All @@ -152,7 +156,7 @@ def _save(im, fp, filename):
else:
colormapfirst, colormaplength, colormapentry = 0, 0, 0

if im.mode == "RGBA":
if im.mode in ("LA", "RGBA"):
flags = 8
else:
flags = 0
Expand Down

0 comments on commit 39fae6e

Please sign in to comment.