Skip to content

Commit e71f7c1

Browse files
authored
Merge pull request #1 from radarhere/write-jpeg-com
Support saving JPEG comments
2 parents e9f4858 + 525c011 commit e71f7c1

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

Tests/test_file_jpeg.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,26 @@ def test_app(self):
8686
assert len(im.applist) == 2
8787

8888
assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0\x00"
89+
assert im.app["COM"] == im.info["comment"]
8990

90-
def test_com_write(self):
91-
dummy_text = "this is a test comment"
91+
def test_comment_write(self):
9292
with Image.open(TEST_FILE) as im:
93-
with BytesIO() as buf:
94-
im.save(buf, format="JPEG")
95-
with Image.open(buf) as im2:
96-
assert im.app["COM"] == im2.app["COM"]
97-
with BytesIO() as buf:
98-
im.save(buf, format="JPEG", comment=dummy_text)
99-
with Image.open(buf) as im2:
100-
assert im2.app["COM"].decode() == dummy_text
93+
assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0\x00"
94+
95+
# Test that existing comment is saved by default
96+
out = BytesIO()
97+
im.save(out, format="JPEG")
98+
with Image.open(out) as reloaded:
99+
assert im.info["comment"] == reloaded.info["comment"]
100+
101+
# Test that a comment argument overrides the default comment
102+
for comment in ("Test comment text", b"Text comment text"):
103+
out = BytesIO()
104+
im.save(out, format="JPEG", comment=comment)
105+
with Image.open(out) as reloaded:
106+
if not isinstance(comment, bytes):
107+
comment = comment.encode()
108+
assert reloaded.info["comment"] == comment
101109

102110
def test_cmyk(self):
103111
# Test CMYK handling. Thanks to Tim and Charlie for test data,

src/PIL/JpegImagePlugin.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,7 @@ def validate_qtables(qtables):
714714

715715
extra = info.get("extra", b"")
716716

717-
comment = info.get("comment")
718-
if comment is None and isinstance(im, JpegImageFile):
719-
comment = im.app.get("COM")
717+
comment = info.get("comment", im.info.get("comment"))
720718
if comment:
721719
if isinstance(comment, str):
722720
comment = comment.encode()
@@ -734,7 +732,7 @@ def validate_qtables(qtables):
734732
icc_profile = icc_profile[MAX_DATA_BYTES_IN_MARKER:]
735733
i = 1
736734
for marker in markers:
737-
size = struct.pack(">H", 2 + ICC_OVERHEAD_LEN + len(marker))
735+
size = o16(2 + ICC_OVERHEAD_LEN + len(marker))
738736
extra += (
739737
b"\xFF\xE2"
740738
+ size

0 commit comments

Comments
 (0)