Merge branch 'master' of github.com:python-imaging/Pillow

This commit is contained in:
Alex Clark 2013-12-28 08:58:09 -05:00
commit 5b260bd48b
2 changed files with 16 additions and 7 deletions

View File

@ -559,24 +559,29 @@ def _save(im, fp, filename, chunk=putchunk, check=0):
palette_bytes += b'\0' palette_bytes += b'\0'
chunk(fp, b"PLTE", palette_bytes) chunk(fp, b"PLTE", palette_bytes)
if "transparency" in im.encoderinfo: transparency = im.encoderinfo.get('transparency',im.info.get('transparency', None))
if transparency:
if im.mode == "P": if im.mode == "P":
# limit to actual palette size # limit to actual palette size
alpha_bytes = 2**bits alpha_bytes = 2**bits
if isinstance(im.encoderinfo["transparency"], bytes): if isinstance(transparency, bytes):
chunk(fp, b"tRNS", im.encoderinfo["transparency"][:alpha_bytes]) chunk(fp, b"tRNS", transparency[:alpha_bytes])
else: else:
transparency = max(0, min(255, im.encoderinfo["transparency"])) transparency = max(0, min(255, transparency))
alpha = b'\xFF' * transparency + b'\0' alpha = b'\xFF' * transparency + b'\0'
chunk(fp, b"tRNS", alpha[:alpha_bytes]) chunk(fp, b"tRNS", alpha[:alpha_bytes])
elif im.mode == "L": elif im.mode == "L":
transparency = max(0, min(65535, im.encoderinfo["transparency"])) transparency = max(0, min(65535, transparency))
chunk(fp, b"tRNS", o16(transparency)) chunk(fp, b"tRNS", o16(transparency))
elif im.mode == "RGB": elif im.mode == "RGB":
red, green, blue = im.encoderinfo["transparency"] red, green, blue = transparency
chunk(fp, b"tRNS", o16(red) + o16(green) + o16(blue)) chunk(fp, b"tRNS", o16(red) + o16(green) + o16(blue))
else: else:
raise IOError("cannot use transparency for this mode") if "transparency" in im.encoderinfo:
# don't bother with transparency if it's an RGBA
# and it's in the info dict. It's probably just stale.
raise IOError("cannot use transparency for this mode")
else: else:
if im.mode == "P" and im.im.getpalettemode() == "RGBA": if im.mode == "P" and im.im.getpalettemode() == "RGBA":
alpha = im.im.getpalette("RGBA", "A") alpha = im.im.getpalette("RGBA", "A")

View File

@ -244,5 +244,9 @@ def test_trns_rgb():
im = Image.open(file) im = Image.open(file)
assert_equal(im.info["transparency"], (248, 248, 248)) assert_equal(im.info["transparency"], (248, 248, 248))
# check saving transparency by default
im = roundtrip(im)
assert_equal(im.info["transparency"], (248, 248, 248))
im = roundtrip(im, transparency=(0, 1, 2)) im = roundtrip(im, transparency=(0, 1, 2))
assert_equal(im.info["transparency"], (0, 1, 2)) assert_equal(im.info["transparency"], (0, 1, 2))