Merge pull request #508 from wiredfool/no-icc-profile

Fix crash on saving PNG when icc_profile is None
This commit is contained in:
wiredfool 2014-01-31 14:03:21 -08:00
commit de1b02dc8c
3 changed files with 23 additions and 7 deletions

View File

@ -605,19 +605,14 @@ 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"
name = b"ICC Profile"
data = name + b"\0\0" + zlib.compress(im.info["icc_profile"])
chunk(fp, b"iCCP", data)

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

@ -250,3 +250,24 @@ def test_trns_rgb():
im = roundtrip(im, transparency=(0, 1, 2))
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_equal(im.info['icc_profile'], None)
im = roundtrip(im)
assert_false('icc_profile' in im.info)
def test_roundtrip_icc_profile():
# check that we can roundtrip the icc profile
im = lena('RGB')
jpeg_image = Image.open('Tests/images/flower2.jpg')
expected_icc = jpeg_image.info['icc_profile']
im.info['icc_profile'] = expected_icc
im = roundtrip(im)
assert_equal(im.info['icc_profile'], expected_icc)