-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Use indexed color for PNG images in PDF files when possible #17895
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
Conversation
When PNG images have 256 colors or fewer, convert them to index color before saving them in a PDF. This can result in a signifcant reduction in file size.
It seems to cause problems with images used for alpha transparency. It also doesn't have any benefit, since Pillow seems to always use 8-bit indexed color, even when 1-, 2-, or 4-bit indexed color is possible.
PNGs separate large images into chunks, but PDFs do not.
The test failure seems to be due to an inconsistency in how the PDF to PNG conversion is done for the tests, with regard to indexed color. The difference image shows ten pixels that differ, but flipping through the two PDFs shows no visible differences. Furthermore, if I convert the PDFs to PNGs using |
I am 👍 on adding to the tolerance here and leaving a note in the tests as to why. Given that we have work going to move away for storing the baseline images in the repository (and instead generate them from the previous commit / external package) it is probably not worth re-generating the pdf. We also do our pdf comparison at a pretty low DPI, that may also be part of the problem here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modulo an explanatory comment on the test justifying the large error and noting that it is only needed for the pdf test.
There are ten pixels that differ due to how the subpixel calculation is done.
I added a comment about the tolerance. I think it's a combination of the low resolution and the fact that Ghostscript doesn't seem to be doing subpixel interpolation; instead of a slight change in value, the change is drastic when it happens. I see a similar issue with one of the tests for the PGF backend when I run the test suite locally. |
The Azure MacOS job has been quite flaky. I think we can merge on two approvals. I'll merge, though I've not carefully looked at this. |
PR Summary
This pull requests modifies the PDF backend to use indexed color for PNG images in PDF files when possible.
When PNG images have 256 colors or fewer, it converts them to indexed color before saving them in a PDF. This can result in a significant reduction in file size in some cases. The is particularly true for raster data that uses a colormap but no interpolation, such as Healpy
mollview
plots (for some of the maps I tested, the new PDFs are less than half the size).Currently, this is only done for RGB images. For grayscale images with 128 colors or fewer, there should also be an improvement, since this allows for 4-bit indexed color (or lower) instead of 8-bit grayscale. However, Pillow seems to always use 8-bit indexed color, so there's no improvement in practice. Edit: The PDF specification requires that soft-mask images must use the
DeviceGray
color space, so they can't be indexed color; this would further complicate extending indexed color to grayscale images, since the PDF backend uses soft-masks.PR Checklist