From e41cfade29727bd8faff7742905f36f26819e5e8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Mar 2020 06:57:29 +1100 Subject: [PATCH] Add JPEG comment to info dictionary --- Tests/test_file_jpeg.py | 2 ++ docs/handbook/image-file-formats.rst | 5 +++++ docs/releasenotes/7.1.0.rst | 9 +++++++++ src/PIL/JpegImagePlugin.py | 1 + 4 files changed, 17 insertions(+) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index f13536d5868..33045122891 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -60,6 +60,8 @@ def test_app(self): ) assert len(im.applist) == 2 + assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0\x00" + def test_cmyk(self): # Test CMYK handling. Thanks to Tim and Charlie for test data, # Michael for getting me to look one more time. diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 7ce685ed2a3..18f547a498c 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -298,6 +298,11 @@ The :py:meth:`~PIL.Image.Image.open` method may set the following **exif** Raw EXIF data from the image. +**comment** + A comment about the image. + + .. versionadded:: 7.1.0 + The :py:meth:`~PIL.Image.Image.save` method supports the following options: diff --git a/docs/releasenotes/7.1.0.rst b/docs/releasenotes/7.1.0.rst index 1369177d26e..e3bc107ddff 100644 --- a/docs/releasenotes/7.1.0.rst +++ b/docs/releasenotes/7.1.0.rst @@ -18,6 +18,15 @@ been resolved. im = Image.open("hopper.jpg") im.save("out.jpg", quality=0) +API Additions +============= + +Reading JPEG comments +^^^^^^^^^^^^^^^^^^^^^ + +When opening a JPEG image, the comment may now be read into +:py:attr:`~PIL.Image.Image.info`. + Other Changes ============= diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 229eac2141e..2aa029efbff 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -176,6 +176,7 @@ def COM(self, marker): n = i16(self.fp.read(2)) - 2 s = ImageFile._safe_read(self.fp, n) + self.info["comment"] = s self.app["COM"] = s # compatibility self.applist.append(("COM", s))