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

Support saving JPEG comments #6774

Merged
merged 12 commits into from Dec 7, 2022
4 changes: 1 addition & 3 deletions src/PIL/JpegImagePlugin.py
Expand Up @@ -736,9 +736,7 @@ def validate_qtables(qtables):
)
i += 1

comment = info.get("comment", im.info.get("comment")) or b""
if isinstance(comment, str):
comment = comment.encode()
comment = info.get("comment", im.info.get("comment"))

# "progressive" is the official name, but older documentation
# says "progression"
Expand Down
2 changes: 1 addition & 1 deletion src/encode.c
Expand Up @@ -1057,7 +1057,7 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {

if (!PyArg_ParseTuple(
args,
"ss|nnnnnnnnOy#y#y#",
"ss|nnnnnnnnOz#y#y#",
smason marked this conversation as resolved.
Show resolved Hide resolved
&mode,
&rawmode,
&quality,
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/JpegEncode.c
Expand Up @@ -278,7 +278,7 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {

case 4:

if (context->comment_size > 0) {
if (context->comment) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rest of the code is conditional on comment != NULL rather than comment_size > 0

they should be the same, but keeping things consistent feels nicer to me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say "the rest of the code", what are you referring to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, that wasn't explained well

PyImaging_JpegEncoderNew seems to imply that comment_size is only valid when comment is non-NULL. e.g. comment is initialised to NULL, but comment_size isn't initialised to zero, and later in function (line 1109) it only sets comment = NULL

jpeg_write_marker(&context->cinfo, JPEG_COM, (unsigned char *)context->comment, context->comment_size);
}
state->state++;
Expand Down