Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIL doesn't write JPEG comment #6773

Closed
smason opened this issue Dec 2, 2022 · 2 comments · Fixed by #6774
Closed

PIL doesn't write JPEG comment #6773

smason opened this issue Dec 2, 2022 · 2 comments · Fixed by #6774
Labels

Comments

@smason
Copy link
Contributor

smason commented Dec 2, 2022

Following up on an answer I gave to the following stackoverflow question: https://stackoverflow.com/a/74658310/1358308

OP asked how to write a JPEG comment into the file, i.e. not an EXIF comment, but as a COM segment. The closest issue I could find, #4450, was about reading JPEG comments.

While looking for a solution I cloned the repo (hash 5bdf63e) to see what was going on, but couldn't see anything relevant.

Would be happy to turn this into a pull-request if you think this is a reasonable idea. I think I'd fix by modifying _save() to pull in the comment (from the applist) by appending to extra in a similar way as icc_profile:

extra = info.get("extra", b"")
icc_profile = info.get("icc_profile")
if icc_profile:
ICC_OVERHEAD_LEN = 14
MAX_BYTES_IN_MARKER = 65533
MAX_DATA_BYTES_IN_MARKER = MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN
markers = []
while icc_profile:
markers.append(icc_profile[:MAX_DATA_BYTES_IN_MARKER])
icc_profile = icc_profile[MAX_DATA_BYTES_IN_MARKER:]
i = 1
for marker in markers:
size = struct.pack(">H", 2 + ICC_OVERHEAD_LEN + len(marker))
extra += (
b"\xFF\xE2"
+ size
+ b"ICC_PROFILE\0"
+ o8(i)
+ o8(len(markers))
+ marker
)
i += 1

Comments appreciated!

@nicbou
Copy link

nicbou commented Jan 29, 2023

Thanks for this patch Sam. It's a significant improvement. Could you add a mention in the documentation?

@radarhere
Copy link
Member

radarhere commented Jan 29, 2023

It should already be mentioned in the documentation, under https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#jpeg-saving, thanks to #6784

Saving
The save() method supports the following options:
...
comment
A comment about the image.

New in version 9.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants