Cleanup handling of ICC profile, more extensive testing

This commit is contained in:
Bruno Renié 2014-01-19 19:09:40 +01:00
parent 0348fcac51
commit e1e64904c7
3 changed files with 20 additions and 8 deletions

View File

@ -605,20 +605,15 @@ def _save(im, fp, filename, chunk=putchunk, check=0):
chunk(fp, cid, data)
# ICC profile writing support -- 2008-06-06 Florian Hoech
if "icc_profile" in im.info:
if im.info.get("icc_profile"):
# ICC profile
# according to PNG spec, the iCCP chunk contains:
# Profile name 1-79 bytes (character string)
# Null separator 1 byte (null character)
# Compression method 1 byte (0)
# Compressed profile n bytes (zlib with deflate compression)
try:
import ICCProfile
p = ICCProfile.ICCProfile(im.info["icc_profile"])
name = p.tags.desc.get("ASCII", p.tags.desc.get("Unicode", p.tags.desc.get("Macintosh", p.tags.desc.get("en", {}).get("US", "ICC Profile")))).encode("latin1", "replace")[:79]
except ImportError:
name = b"ICC Profile"
data = name or b'ICC Profile' + b"\0\0" + zlib.compress(im.info["icc_profile"])
name = b"ICC Profile"
data = name + b"\0\0" + zlib.compress(im.info["icc_profile"])
chunk(fp, b"iCCP", data)
ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)])

BIN
Tests/images/trollface.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -252,8 +252,25 @@ def test_trns_rgb():
assert_equal(im.info["transparency"], (0, 1, 2))
def test_save_icc_profile_none():
# check saving files with an ICC profile set to None (omit profile)
in_file = "Tests/images/icc_profile_none.png"
im = Image.open(in_file)
assert im.info['icc_profile'] is None
file = tempfile("temp.png")
assert_no_exception(lambda: im.save(file))
im = Image.open(file)
assert 'icc_profile' not in im.info
def test_save_icc_profile_exists():
# check icc profile is kept during save()
in_file = "Tests/images/trollface.png"
im = Image.open(in_file)
orig_profile = im.info['icc_profile']
assert len(orig_profile) == 6636
file = tempfile("temp.png")
assert_no_exception(lambda: im.save(file))
im = Image.open(file)
assert im.info['icc_profile'] == orig_profile