Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added orientation, compression and id_section as TGA save keyword arguments #3327

Merged
merged 3 commits into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 75 additions & 16 deletions Tests/test_file_tga.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def test_sanity(self):
# Generate a new test name every time so the
# test will not fail with permission error
# on Windows.
test_file = self.tempfile("temp.tga")
out = self.tempfile("temp.tga")

original_im.save(test_file, rle=rle)
saved_im = Image.open(test_file)
original_im.save(out, rle=rle)
saved_im = Image.open(out)
if rle:
self.assertEqual(
saved_im.info["compression"],
Expand Down Expand Up @@ -95,34 +95,93 @@ def test_save(self):
test_file = "Tests/images/tga_id_field.tga"
im = Image.open(test_file)

test_file = self.tempfile("temp.tga")
out = self.tempfile("temp.tga")

# Save
im.save(test_file)
test_im = Image.open(test_file)
im.save(out)
test_im = Image.open(out)
self.assertEqual(test_im.size, (100, 100))
self.assertEqual(test_im.info["id_section"], im.info["id_section"])

# RGBA save
im.convert("RGBA").save(test_file)
test_im = Image.open(test_file)
im.convert("RGBA").save(out)
test_im = Image.open(out)
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")

# Save with custom id section greater than 255 characters
id_section = b"Test content" * 25
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)

def test_save_rle(self):
test_file = "Tests/images/rgb32rle.tga"
im = Image.open(test_file)
self.assertEqual(im.info["compression"], "tga_rle")

test_file = self.tempfile("temp.tga")
out = self.tempfile("temp.tga")

# Save
im.save(test_file)
test_im = Image.open(test_file)
im.save(out)
test_im = Image.open(out)
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
im.convert("RGBA").save(test_file)
test_im = Image.open(test_file)
im.convert("RGBA").save(out)
test_im = Image.open(out)
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):
# There are 559 transparent pixels in la.tga.
num_transparent = 559
Expand All @@ -133,10 +192,10 @@ def test_save_l_transparency(self):
self.assertEqual(
im.getchannel("A").getcolors()[0][0], num_transparent)

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

test_im = Image.open(test_file)
test_im = Image.open(out)
self.assertEqual(test_im.mode, "LA")
self.assertEqual(
test_im.getchannel("A").getcolors()[0][0], num_transparent)
Expand Down
32 changes: 25 additions & 7 deletions src/PIL/TgaImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from . import Image, ImageFile, ImagePalette
from ._binary import i8, i16le as i16, o8, o16le as o16

import warnings

__version__ = "0.3"


Expand Down Expand Up @@ -53,7 +55,7 @@ def _open(self):
# process header
s = self.fp.read(18)

idlen = i8(s[0])
id_len = i8(s[0])

colormaptype = i8(s[1])
imagetype = i8(s[2])
Expand Down Expand Up @@ -100,8 +102,8 @@ def _open(self):
if imagetype & 8:
self.info["compression"] = "tga_rle"

if idlen:
self.info["id_section"] = self.fp.read(idlen)
if id_len:
self.info["id_section"] = self.fp.read(id_len)

if colormaptype:
# read palette
Expand Down Expand Up @@ -151,11 +153,23 @@ def _save(im, fp, filename):
except KeyError:
raise IOError("cannot write mode %s as TGA" % im.mode)

rle = im.encoderinfo.get("rle", False)

if "rle" in im.encoderinfo:
Copy link
Contributor

@danpla danpla Sep 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe to remove the "rle" keyword in favor of "compression".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I'm just not keen to break the backwards compatibility of this one. If this PR is merged, you're welcome to submit a separate PR for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds sensible to keep it, we've no idea what code might be using im.encoderinfo["rle"].

We'd need deprecation warnings first before removing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TGA-specifc flags are not currently documented, so it's unlikely that anyone uses them.

rle = im.encoderinfo["rle"]
else:
compression = im.encoderinfo.get("compression",
im.info.get("compression"))
rle = compression == "tga_rle"
if rle:
imagetype += 8

id_section = im.encoderinfo.get("id_section",
im.info.get("id_section", ""))
id_len = len(id_section)
if id_len > 255:
id_len = 255
id_section = id_section[:255]
warnings.warn("id_section has been trimmed to 255 characters")

if colormaptype:
colormapfirst, colormaplength, colormapentry = 0, 256, 24
else:
Expand All @@ -166,11 +180,12 @@ def _save(im, fp, filename):
else:
flags = 0

orientation = im.info.get("orientation", -1)
orientation = im.encoderinfo.get("orientation",
im.info.get("orientation", -1))
if orientation > 0:
flags = flags | 0x20

fp.write(b"\000" +
fp.write(o8(id_len) +
o8(colormaptype) +
o8(imagetype) +
o16(colormapfirst) +
Expand All @@ -183,6 +198,9 @@ def _save(im, fp, filename):
o8(bits) +
o8(flags))

if id_section:
fp.write(id_section)

if colormaptype:
fp.write(im.im.getpalette("RGB", "BGR"))

Expand Down