diff --git a/Tests/images/la.tga b/Tests/images/la.tga new file mode 100644 index 000000000..8c83104ed Binary files /dev/null and b/Tests/images/la.tga differ diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index ef3acfe65..da92cdf28 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -41,9 +41,6 @@ class TestFileTga(PillowTestCase): 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) @@ -60,8 +57,25 @@ class TestFileTga(PillowTestCase): 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__': diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index dd1a3d2bc..7f2836f54 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -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 ^^^^ @@ -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 ^^^ diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py index 76d9ba8de..33b9a3e38 100644 --- a/src/PIL/TgaImagePlugin.py +++ b/src/PIL/TgaImagePlugin.py @@ -33,6 +33,7 @@ MODES = { (1, 8): "P", (3, 1): "1", (3, 8): "L", + (3, 16): "LA", (2, 16): "BGR;5", (2, 24): "BGR", (2, 32): "BGRA", @@ -74,6 +75,8 @@ class TgaImageFile(ImageFile.ImageFile): 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): @@ -134,6 +137,7 @@ class TgaImageFile(ImageFile.ImageFile): 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), @@ -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