TGA: Read and write LA data

This commit is contained in:
Daniel Plakhotich 2018-06-14 12:18:08 +03:00
parent 956432ecdf
commit 39fae6e077
4 changed files with 31 additions and 11 deletions

BIN
Tests/images/la.tga Normal file

Binary file not shown.

View File

@ -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__':

View File

@ -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
^^^

View File

@ -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