mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 10:16:17 +03:00
Added orientation, compression and id_section as keyword arguments
This commit is contained in:
parent
41954f2447
commit
9e4c54e10f
|
@ -53,10 +53,10 @@ class TestFileTga(PillowTestCase):
|
||||||
# Generate a new test name every time so the
|
# Generate a new test name every time so the
|
||||||
# test will not fail with permission error
|
# test will not fail with permission error
|
||||||
# on Windows.
|
# on Windows.
|
||||||
test_file = self.tempfile("temp.tga")
|
out = self.tempfile("temp.tga")
|
||||||
|
|
||||||
original_im.save(test_file, rle=rle)
|
original_im.save(out, rle=rle)
|
||||||
saved_im = Image.open(test_file)
|
saved_im = Image.open(out)
|
||||||
if rle:
|
if rle:
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
saved_im.info["compression"],
|
saved_im.info["compression"],
|
||||||
|
@ -95,34 +95,86 @@ class TestFileTga(PillowTestCase):
|
||||||
test_file = "Tests/images/tga_id_field.tga"
|
test_file = "Tests/images/tga_id_field.tga"
|
||||||
im = Image.open(test_file)
|
im = Image.open(test_file)
|
||||||
|
|
||||||
test_file = self.tempfile("temp.tga")
|
out = self.tempfile("temp.tga")
|
||||||
|
|
||||||
# Save
|
# Save
|
||||||
im.save(test_file)
|
im.save(out)
|
||||||
test_im = Image.open(test_file)
|
test_im = Image.open(out)
|
||||||
self.assertEqual(test_im.size, (100, 100))
|
self.assertEqual(test_im.size, (100, 100))
|
||||||
|
self.assertEqual(test_im.info["id_section"], im.info["id_section"])
|
||||||
|
|
||||||
# RGBA save
|
# RGBA save
|
||||||
im.convert("RGBA").save(test_file)
|
im.convert("RGBA").save(out)
|
||||||
test_im = Image.open(test_file)
|
test_im = Image.open(out)
|
||||||
self.assertEqual(test_im.size, (100, 100))
|
self.assertEqual(test_im.size, (100, 100))
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
def test_save_rle(self):
|
def test_save_rle(self):
|
||||||
test_file = "Tests/images/rgb32rle.tga"
|
test_file = "Tests/images/rgb32rle.tga"
|
||||||
im = Image.open(test_file)
|
im = Image.open(test_file)
|
||||||
|
self.assertEqual(im.info["compression"], "tga_rle")
|
||||||
|
|
||||||
test_file = self.tempfile("temp.tga")
|
out = self.tempfile("temp.tga")
|
||||||
|
|
||||||
# Save
|
# Save
|
||||||
im.save(test_file)
|
im.save(out)
|
||||||
test_im = Image.open(test_file)
|
test_im = Image.open(out)
|
||||||
self.assertEqual(test_im.size, (199, 199))
|
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)
|
||||||
|
|
||||||
# RGBA save
|
# RGBA save
|
||||||
im.convert("RGBA").save(test_file)
|
im.convert("RGBA").save(out)
|
||||||
test_im = Image.open(test_file)
|
test_im = Image.open(out)
|
||||||
self.assertEqual(test_im.size, (199, 199))
|
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")
|
||||||
|
|
||||||
def test_save_l_transparency(self):
|
def test_save_l_transparency(self):
|
||||||
# There are 559 transparent pixels in la.tga.
|
# There are 559 transparent pixels in la.tga.
|
||||||
num_transparent = 559
|
num_transparent = 559
|
||||||
|
@ -133,10 +185,10 @@ class TestFileTga(PillowTestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
im.getchannel("A").getcolors()[0][0], num_transparent)
|
im.getchannel("A").getcolors()[0][0], num_transparent)
|
||||||
|
|
||||||
test_file = self.tempfile("temp.tga")
|
out = self.tempfile("temp.tga")
|
||||||
im.save(test_file)
|
im.save(out)
|
||||||
|
|
||||||
test_im = Image.open(test_file)
|
test_im = Image.open(out)
|
||||||
self.assertEqual(test_im.mode, "LA")
|
self.assertEqual(test_im.mode, "LA")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
test_im.getchannel("A").getcolors()[0][0], num_transparent)
|
test_im.getchannel("A").getcolors()[0][0], num_transparent)
|
||||||
|
|
|
@ -151,11 +151,19 @@ def _save(im, fp, filename):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise IOError("cannot write mode %s as TGA" % im.mode)
|
raise IOError("cannot write mode %s as TGA" % im.mode)
|
||||||
|
|
||||||
rle = im.encoderinfo.get("rle", False)
|
if "rle" in im.encoderinfo:
|
||||||
|
rle = im.encoderinfo["rle"]
|
||||||
|
else:
|
||||||
|
compression = im.encoderinfo.get("compression",
|
||||||
|
im.info.get("compression"))
|
||||||
|
rle = compression == "tga_rle"
|
||||||
if rle:
|
if rle:
|
||||||
imagetype += 8
|
imagetype += 8
|
||||||
|
|
||||||
|
id_section = im.encoderinfo.get("id_section",
|
||||||
|
im.info.get("id_section", ""))
|
||||||
|
idlen = len(id_section)
|
||||||
|
|
||||||
if colormaptype:
|
if colormaptype:
|
||||||
colormapfirst, colormaplength, colormapentry = 0, 256, 24
|
colormapfirst, colormaplength, colormapentry = 0, 256, 24
|
||||||
else:
|
else:
|
||||||
|
@ -166,11 +174,12 @@ def _save(im, fp, filename):
|
||||||
else:
|
else:
|
||||||
flags = 0
|
flags = 0
|
||||||
|
|
||||||
orientation = im.info.get("orientation", -1)
|
orientation = im.encoderinfo.get("orientation",
|
||||||
|
im.info.get("orientation", -1))
|
||||||
if orientation > 0:
|
if orientation > 0:
|
||||||
flags = flags | 0x20
|
flags = flags | 0x20
|
||||||
|
|
||||||
fp.write(b"\000" +
|
fp.write(o8(idlen) +
|
||||||
o8(colormaptype) +
|
o8(colormaptype) +
|
||||||
o8(imagetype) +
|
o8(imagetype) +
|
||||||
o16(colormapfirst) +
|
o16(colormapfirst) +
|
||||||
|
@ -183,6 +192,9 @@ def _save(im, fp, filename):
|
||||||
o8(bits) +
|
o8(bits) +
|
||||||
o8(flags))
|
o8(flags))
|
||||||
|
|
||||||
|
if id_section:
|
||||||
|
fp.write(id_section)
|
||||||
|
|
||||||
if colormaptype:
|
if colormaptype:
|
||||||
fp.write(im.im.getpalette("RGB", "BGR"))
|
fp.write(im.im.getpalette("RGB", "BGR"))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user