diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index f3738bd24..a6038d9f2 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -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)]) diff --git a/Tests/images/trollface.png b/Tests/images/trollface.png new file mode 100644 index 000000000..f90ebe545 Binary files /dev/null and b/Tests/images/trollface.png differ diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 99b00f718..73a3fff15 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -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