From 25d961238c68a614dc7aa6bb5e55048ac40e346d Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Wed, 20 Dec 2017 11:28:50 +0000 Subject: [PATCH] Added sRGB and cHRM chunks to PngInfo, added tests for #2782 --- PIL/PngImagePlugin.py | 21 ++++++++++++++++++++- Tests/test_file_png.py | 18 +----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 2e2600166..d7cf97ea4 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -387,12 +387,31 @@ class PngStream(ChunkStream): return s def chunk_gAMA(self, pos, length): - # gamma setting s = ImageFile._safe_read(self.fp, length) self.im_info["gamma"] = i32(s) / 100000.0 return s + def chunk_cHRM(self, pos, length): + # chromaticity, 8 unsigned ints, actual value is scaled by 100000 + # WP x,y, Red x,y, Green x,y Blue x,y + + s = ImageFile._safe_read(self.fp, length) + raw_vals = struct.unpack('>%dI' % (len(s) // 4), s) + self.im_info['chromaticity'] = tuple(elt/100000.0 for elt in raw_vals) + return s + + def chunk_sRGB(self, pos, length): + # srgb rendering intent, 1 byte + # 0 perceptual + # 1 relative folorimetric + # 2 saturation + # 3 absolute colorimetric + + s = ImageFile._safe_read(self.fp, length) + self.im_info['srgb'] = i8(s) + return s + def chunk_pHYs(self, pos, length): # pixels per unit diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 094910dc0..5a044eb82 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -53,8 +53,7 @@ class TestFilePng(PillowTestCase): chunks = [] with open(filename, "rb") as fp: fp.read(8) - png = PngImagePlugin.PngStream(fp) - while True: + while PngImagePlugin.PngStream(fp) as png: cid, pos, length = png.read() chunks.append(cid) try: @@ -516,22 +515,7 @@ class TestFilePng(PillowTestCase): test_file = self.tempfile("temp.png") im.convert("P").save(test_file, dpi=(100, 100)) -<<<<<<< 1cf2deba3bdb3ea93fdd859c0f052dfcea898c52 - chunks = [] - with open(test_file, "rb") as fp: - fp.read(8) - with PngImagePlugin.PngStream(fp) as png: - while True: - cid, pos, length = png.read() - chunks.append(cid) - try: - s = png.call(cid, pos, length) - except EOFError: - break - png.crc(cid, s) -======= chunks = self.get_chunks(test_file) ->>>>>>> refactor out get_chunks # https://www.w3.org/TR/PNG/#5ChunkOrdering # IHDR - shall be first