change: encode non-JPEG images as PNGs instead of JPEG2000 images #1481
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This uses Pillow to re-encode any non-JPEG image as a PNG, then inline that
image's IDAT chunks as a FlateDecode value, which allows us to reuse the work
from the PNG encoder. This means we'll reencode some PNGs we could have passed
through directly, but that could be changed later. Alpha layers continue to be
handled separately, as appears to be required by the PDF spec.
Provides a potential fix for #1444, which works in local tests (and appears to pass the existing test suite, at least locally).
There's a lingering optimization where we could directly pass through PNGs that meet certain criteria:
optimize_size
could lead us to push Pillow to re-encode the image, although I suspect this would rarely show a benefit and could increase sizes in already-extra-compressed images.Because that requires more investigation and work, I haven't done it here, this is mostly just the minimum effort to get the change out the door. It could be worth filing an issue to investigate "PNG passthrough" for the future, but this solves my immediate concerns of JPEG2000 being slow. I expect - but haven't thoroughly tested - that it may also resolve issues like #1475. (For the example in #1462, the output PDF with this patch is 6.06 KiB, which is bigger than version 52, but much smaller than version 53.0.)