mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 17:54:32 +03:00
Moved iCCP chunk before PLTE chunk when saving as PNG
This commit is contained in:
parent
da8f2737a8
commit
361f579579
|
@ -715,6 +715,18 @@ def _save(im, fp, filename, chunk=putchunk, check=0):
|
||||||
b'\0', # 11: filter category
|
b'\0', # 11: filter category
|
||||||
b'\0') # 12: interlace flag
|
b'\0') # 12: interlace flag
|
||||||
|
|
||||||
|
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)
|
||||||
|
# Null separator 1 byte (null character)
|
||||||
|
# Compression method 1 byte (0)
|
||||||
|
# Compressed profile n bytes (zlib with deflate compression)
|
||||||
|
name = b"ICC Profile"
|
||||||
|
data = name + b"\0\0" + zlib.compress(icc)
|
||||||
|
chunk(fp, b"iCCP", data)
|
||||||
|
|
||||||
if im.mode == "P":
|
if im.mode == "P":
|
||||||
palette_byte_number = (2 ** bits) * 3
|
palette_byte_number = (2 ** bits) * 3
|
||||||
palette_bytes = im.im.getpalette("RGB")[:palette_byte_number]
|
palette_bytes = im.im.getpalette("RGB")[:palette_byte_number]
|
||||||
|
@ -764,18 +776,6 @@ 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 = 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)
|
|
||||||
# Null separator 1 byte (null character)
|
|
||||||
# Compression method 1 byte (0)
|
|
||||||
# Compressed profile n bytes (zlib with deflate compression)
|
|
||||||
name = b"ICC Profile"
|
|
||||||
data = name + b"\0\0" + zlib.compress(icc)
|
|
||||||
chunk(fp, b"iCCP", data)
|
|
||||||
|
|
||||||
ImageFile._save(im, _idat(fp, chunk),
|
ImageFile._save(im, _idat(fp, chunk),
|
||||||
[("zip", (0, 0)+im.size, 0, rawmode)])
|
[("zip", (0, 0)+im.size, 0, rawmode)])
|
||||||
|
|
||||||
|
|
|
@ -497,6 +497,25 @@ class TestFilePng(PillowTestCase):
|
||||||
self.assertEqual(repr_png.format, 'PNG')
|
self.assertEqual(repr_png.format, 'PNG')
|
||||||
self.assert_image_equal(im, repr_png)
|
self.assert_image_equal(im, repr_png)
|
||||||
|
|
||||||
|
def test_chunk_order(self):
|
||||||
|
im = Image.open("Tests/images/icc_profile.png")
|
||||||
|
test_file = self.tempfile("temp.png")
|
||||||
|
im.convert("P").save(test_file)
|
||||||
|
|
||||||
|
chunks = []
|
||||||
|
fp = open(test_file, "rb")
|
||||||
|
fp.read(8)
|
||||||
|
png = PngImagePlugin.PngStream(fp)
|
||||||
|
while True:
|
||||||
|
cid, pos, length = png.read()
|
||||||
|
chunks.append(cid)
|
||||||
|
try:
|
||||||
|
s = png.call(cid, pos, length)
|
||||||
|
except EOFError:
|
||||||
|
break
|
||||||
|
png.crc(cid, s)
|
||||||
|
self.assertLess(chunks.index(b"iCCP"), chunks.index(b"PLTE"))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user