Merge pull request #1909 from uploadcare/png-icc-profile

Get ICC profile from `encoderinfo` while PNG saving
This commit is contained in:
wiredfool 2016-06-25 11:01:53 +01:00 committed by GitHub
commit 6a3acde001
3 changed files with 28 additions and 18 deletions

View File

@ -755,8 +755,8 @@ def _save(im, fp, filename, chunk=putchunk, check=0):
for cid, data in info.chunks: for cid, data in info.chunks:
chunk(fp, cid, data) chunk(fp, cid, data)
# ICC profile writing support -- 2008-06-06 Florian Hoech icc = im.encoderinfo.get("icc_profile", im.info.get("icc_profile"))
if im.info.get("icc_profile"): if icc:
# ICC profile # ICC profile
# according to PNG spec, the iCCP chunk contains: # according to PNG spec, the iCCP chunk contains:
# Profile name 1-79 bytes (character string) # 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) # Compression method 1 byte (0)
# Compressed profile n bytes (zlib with deflate compression) # Compressed profile n bytes (zlib with deflate compression)
name = b"ICC Profile" 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) chunk(fp, b"iCCP", data)
ImageFile._save(im, _idat(fp, chunk), ImageFile._save(im, _idat(fp, chunk),

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

@ -434,26 +434,36 @@ class TestFilePng(PillowTestCase):
self.assertEqual(im.info["transparency"], 0) self.assertEqual(im.info["transparency"], 0)
def test_save_icc_profile_none(self): def test_save_icc_profile(self):
# check saving files with an ICC profile set to None (omit profile) im = Image.open("Tests/images/icc_profile_none.png")
in_file = "Tests/images/icc_profile_none.png" self.assertEqual(im.info['icc_profile'], None)
im = Image.open(in_file)
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) self.assertEqual(im.info['icc_profile'], None)
im = roundtrip(im) im = roundtrip(im)
self.assertNotIn('icc_profile', im.info) 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): def test_repr_png(self):
im = hopper() im = hopper()