From 365d5e541ac9567756e4594e508776a2133d076a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 23 Feb 2019 13:30:38 +1100 Subject: [PATCH] Added EXIF support --- Tests/test_file_png.py | 19 +++++++++++++++++++ src/PIL/PngImagePlugin.py | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 2b80bf357..0e912c1ee 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -590,6 +590,25 @@ class TestFilePng(PillowTestCase): im = Image.open("Tests/images/hopper_idat_after_image_end.png") self.assertEqual(im.text, {'TXT': 'VALUE', 'ZIP': 'VALUE'}) + def test_exif(self): + im = Image.open("Tests/images/pil_sample_rgb.jpg") + + test_file = self.tempfile("temp.png") + im.save(test_file) + + reloaded = Image.open(test_file) + exif = reloaded._getexif() + self.assertEqual(exif[305], "Adobe Photoshop CS Macintosh") + + def test_exif_argument(self): + im = Image.open(TEST_PNG_FILE) + + test_file = self.tempfile("temp.png") + im.save(test_file, exif=b"exifstring") + + reloaded = Image.open(test_file) + self.assertEqual(reloaded.info["exif"], b"Exif\x00\x00exifstring") + @unittest.skipUnless(HAVE_WEBP and _webp.HAVE_WEBPANIM, "WebP support not installed with animation") def test_apng(self): diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index e04ae2274..2145e8ca7 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -529,6 +529,11 @@ class PngStream(ChunkStream): return s + def chunk_eXIf(self, pos, length): + s = ImageFile._safe_read(self.fp, length) + self.im_info["exif"] = b"Exif\x00\x00"+s + return s + # APNG chunks def chunk_acTL(self, pos, length): s = ImageFile._safe_read(self.fp, length) @@ -683,6 +688,10 @@ class PngImageFile(ImageFile.ImageFile): self.png.close() self.png = None + def _getexif(self): + from .JpegImagePlugin import _getexif + return _getexif(self) + # -------------------------------------------------------------------- # PNG writer @@ -861,6 +870,12 @@ def _save(im, fp, filename, chunk=putchunk): chunks.remove(cid) chunk(fp, cid, data) + exif = im.encoderinfo.get("exif", im.info.get("exif")) + if exif: + if exif.startswith(b"Exif\x00\x00"): + exif = exif[6:] + chunk(fp, b"eXIf", exif) + ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0)+im.size, 0, rawmode)])