| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
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 mutool instead of gs, the images are identical. |
Sorry, something went wrong.
|
The difference image only differs in single pixels. This is some kind of subpixel calculation issue. It's ok to slightly increase the tolerance for the test @image_comparison(..., tol=...) to make it pass. |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
There was a problem hiding this comment.
Modulo an explanatory comment on the test justifying the large error and noting that it is only needed for the pdf test.
Sorry, something went wrong.
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. |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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