diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 0e98dbf76..bfad17683 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -755,8 +755,8 @@ def _save(im, fp, filename, chunk=putchunk, check=0): for cid, data in info.chunks: chunk(fp, cid, data) - # ICC profile writing support -- 2008-06-06 Florian Hoech - if im.info.get("icc_profile"): + icc = im.encoderinfo.get("icc_profile", im.info.get("icc_profile")) + if icc: # ICC profile # according to PNG spec, the iCCP chunk contains: # Profile name 1-79 bytes (character string) @@ -764,7 +764,7 @@ def _save(im, fp, filename, chunk=putchunk, check=0): # Compression method 1 byte (0) # Compressed profile n bytes (zlib with deflate compression) name = b"ICC Profile" - data = name + b"\0\0" + zlib.compress(im.info["icc_profile"]) + data = name + b"\0\0" + zlib.compress(icc) chunk(fp, b"iCCP", data) ImageFile._save(im, _idat(fp, chunk), diff --git a/Tests/images/icc_profile.png b/Tests/images/icc_profile.png new file mode 100644 index 000000000..4bf92a1d3 Binary files /dev/null and b/Tests/images/icc_profile.png differ diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 0ba51865c..562d13d79 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -434,26 +434,36 @@ class TestFilePng(PillowTestCase): self.assertEqual(im.info["transparency"], 0) - def test_save_icc_profile_none(self): - # 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) + def test_save_icc_profile(self): + im = Image.open("Tests/images/icc_profile_none.png") + self.assertEqual(im.info['icc_profile'], None) + + with_icc = Image.open("Tests/images/icc_profile.png") + expected_icc = with_icc.info['icc_profile'] + + im = roundtrip(im, icc_profile=expected_icc) + self.assertEqual(im.info['icc_profile'], expected_icc) + + def test_discard_icc_profile(self): + im = Image.open('Tests/images/icc_profile.png') + + im = roundtrip(im, icc_profile=None) + self.assertNotIn('icc_profile', im.info) + + def test_roundtrip_icc_profile(self): + im = Image.open('Tests/images/icc_profile.png') + expected_icc = im.info['icc_profile'] + + im = roundtrip(im) + self.assertEqual(im.info['icc_profile'], expected_icc) + + def test_roundtrip_no_icc_profile(self): + im = Image.open("Tests/images/icc_profile_none.png") self.assertEqual(im.info['icc_profile'], None) im = roundtrip(im) self.assertNotIn('icc_profile', im.info) - def test_roundtrip_icc_profile(self): - # check that we can roundtrip the icc profile - im = hopper('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) - self.assertEqual(im.info['icc_profile'], expected_icc) - def test_repr_png(self): im = hopper()