Pillow/Tests/test_file_tga.py

201 lines
6.5 KiB
Python
Raw Normal View History

2018-06-15 23:01:06 +03:00
import os
from glob import glob
from itertools import product
2014-08-20 10:32:06 +04:00
from PIL import Image
from .helper import PillowTestCase
2014-08-20 10:32:06 +04:00
2018-06-15 23:01:06 +03:00
_TGA_DIR = os.path.join("Tests", "images", "tga")
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
2014-08-20 10:39:11 +04:00
class TestFileTga(PillowTestCase):
2014-08-20 10:32:06 +04:00
2018-07-02 00:50:02 +03:00
_MODES = ("L", "LA", "P", "RGB", "RGBA")
2018-06-15 23:01:06 +03:00
_ORIGINS = ("tl", "bl")
2019-06-13 18:54:11 +03:00
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}
2018-06-15 23:01:06 +03:00
def test_sanity(self):
for mode in self._MODES:
png_paths = glob(
2019-06-13 18:54:11 +03:00
os.path.join(_TGA_DIR_COMMON, "*x*_{}.png".format(mode.lower()))
)
2018-06-15 23:01:06 +03:00
for png_path in png_paths:
reference_im = Image.open(png_path)
self.assertEqual(reference_im.mode, mode)
path_no_ext = os.path.splitext(png_path)[0]
for origin, rle in product(self._ORIGINS, (True, False)):
tga_path = "{}_{}_{}.tga".format(
2019-06-13 18:54:11 +03:00
path_no_ext, origin, "rle" if rle else "raw"
)
2018-06-15 23:01:06 +03:00
original_im = Image.open(tga_path)
2019-03-04 07:02:47 +03:00
self.assertEqual(original_im.format, "TGA")
self.assertEqual(original_im.get_format_mimetype(), "image/x-tga")
2018-06-15 23:01:06 +03:00
if rle:
2019-06-13 18:54:11 +03:00
self.assertEqual(original_im.info["compression"], "tga_rle")
2018-06-15 23:01:06 +03:00
self.assertEqual(
original_im.info["orientation"],
2019-06-13 18:54:11 +03:00
self._ORIGIN_TO_ORIENTATION[origin],
)
2018-06-15 23:01:06 +03:00
if mode == "P":
self.assertEqual(
2019-06-13 18:54:11 +03:00
original_im.getpalette(), reference_im.getpalette()
)
2018-06-15 23:01:06 +03:00
self.assert_image_equal(original_im, reference_im)
# Generate a new test name every time so the
# test will not fail with permission error
# on Windows.
out = self.tempfile("temp.tga")
2018-06-15 23:01:06 +03:00
original_im.save(out, rle=rle)
saved_im = Image.open(out)
2018-06-15 23:01:06 +03:00
if rle:
self.assertEqual(
saved_im.info["compression"],
2019-06-13 18:54:11 +03:00
original_im.info["compression"],
)
2018-06-15 23:01:06 +03:00
self.assertEqual(
2019-06-13 18:54:11 +03:00
saved_im.info["orientation"], original_im.info["orientation"]
)
2018-06-15 23:01:06 +03:00
if mode == "P":
self.assertEqual(
2019-06-13 18:54:11 +03:00
saved_im.getpalette(), original_im.getpalette()
)
2018-06-15 23:01:06 +03:00
self.assert_image_equal(saved_im, original_im)
2014-08-20 10:39:11 +04:00
def test_id_field(self):
2014-08-20 10:32:06 +04:00
# tga file with id field
test_file = "Tests/images/tga_id_field.tga"
# Act
im = Image.open(test_file)
# Assert
self.assertEqual(im.size, (100, 100))
2016-04-21 17:40:20 +03:00
def test_id_field_rle(self):
# tga file with id field
test_file = "Tests/images/rgb32rle.tga"
# Act
im = Image.open(test_file)
# Assert
self.assertEqual(im.size, (199, 199))
2015-07-03 08:03:25 +03:00
def test_save(self):
test_file = "Tests/images/tga_id_field.tga"
im = Image.open(test_file)
out = self.tempfile("temp.tga")
2015-07-03 08:03:25 +03:00
# Save
im.save(out)
test_im = Image.open(out)
2015-07-03 08:03:25 +03:00
self.assertEqual(test_im.size, (100, 100))
self.assertEqual(test_im.info["id_section"], im.info["id_section"])
2015-07-03 08:03:25 +03:00
# RGBA save
im.convert("RGBA").save(out)
test_im = Image.open(out)
2015-07-03 08:03:25 +03:00
self.assertEqual(test_im.size, (100, 100))
2015-07-03 09:22:56 +03:00
def test_save_id_section(self):
test_file = "Tests/images/rgb32rle.tga"
im = Image.open(test_file)
out = self.tempfile("temp.tga")
# Check there is no id section
im.save(out)
test_im = Image.open(out)
self.assertNotIn("id_section", test_im.info)
# Save with custom id section
im.save(out, id_section=b"Test content")
test_im = Image.open(out)
self.assertEqual(test_im.info["id_section"], b"Test content")
# Save with custom id section greater than 255 characters
id_section = b"Test content" * 25
2019-06-13 18:54:11 +03:00
self.assert_warning(UserWarning, lambda: im.save(out, id_section=id_section))
test_im = Image.open(out)
self.assertEqual(test_im.info["id_section"], id_section[:255])
test_file = "Tests/images/tga_id_field.tga"
im = Image.open(test_file)
# Save with no id section
im.save(out, id_section="")
test_im = Image.open(out)
self.assertNotIn("id_section", test_im.info)
def test_save_orientation(self):
test_file = "Tests/images/rgb32rle.tga"
im = Image.open(test_file)
self.assertEqual(im.info["orientation"], -1)
out = self.tempfile("temp.tga")
im.save(out, orientation=1)
test_im = Image.open(out)
self.assertEqual(test_im.info["orientation"], 1)
2016-04-21 17:40:20 +03:00
def test_save_rle(self):
test_file = "Tests/images/rgb32rle.tga"
im = Image.open(test_file)
self.assertEqual(im.info["compression"], "tga_rle")
2016-04-21 17:40:20 +03:00
out = self.tempfile("temp.tga")
2016-04-21 17:40:20 +03:00
# Save
im.save(out)
test_im = Image.open(out)
2016-04-21 17:40:20 +03:00
self.assertEqual(test_im.size, (199, 199))
self.assertEqual(test_im.info["compression"], "tga_rle")
# Save without compression
im.save(out, compression=None)
test_im = Image.open(out)
self.assertNotIn("compression", test_im.info)
2016-04-21 17:40:20 +03:00
# RGBA save
im.convert("RGBA").save(out)
test_im = Image.open(out)
2016-04-21 17:40:20 +03:00
self.assertEqual(test_im.size, (199, 199))
test_file = "Tests/images/tga_id_field.tga"
im = Image.open(test_file)
self.assertNotIn("compression", im.info)
# Save with compression
im.save(out, compression="tga_rle")
test_im = Image.open(out)
self.assertEqual(test_im.info["compression"], "tga_rle")
2018-06-14 12:18:08 +03:00
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")
2019-06-13 18:54:11 +03:00
self.assertEqual(im.getchannel("A").getcolors()[0][0], num_transparent)
2018-06-14 12:18:08 +03:00
out = self.tempfile("temp.tga")
im.save(out)
2018-06-14 12:18:08 +03:00
test_im = Image.open(out)
2018-06-14 12:18:08 +03:00
self.assertEqual(test_im.mode, "LA")
2019-06-13 18:54:11 +03:00
self.assertEqual(test_im.getchannel("A").getcolors()[0][0], num_transparent)
2018-06-14 12:18:08 +03:00
self.assert_image_equal(im, test_im)