Repository navigation
ENH: Use png predictors when compressing images in pdf files - #4605
Conversation
It used to work only with RGBA data. Also ensure that the input is contiguous, since it sends row pointers to libpng, which reads each row directly from memory instead of using numpy methods. Improve the wording of a rarely occurring error message: if setjmp returns nonzero, it's not an error in setjmp, it's the result of a longjmp call.
The PDF format allows for PNG predictors in Flate-compressed streams, so we can use libpng to encode a png file and extract the row data from that.
There was a problem hiding this comment.
when did this function become available in numpy?
There was a problem hiding this comment.
I'm not sure, but it just basically calls array(..., order='C') with some additional arguments that don't matter here, so I used that instead.
Not sure when ascontiguousarray was introduced, order='C' should work in any version of numpy. Combine the very similar _gray and _rgb methods into one, and add some docstrings.
There was a problem hiding this comment.
The bug in my initial version was that I used 'Smask' instead of 'SMask' here.
Apparently needed in Python 2.6.
There was a problem hiding this comment.
just as a sanity check, when did this form of transpose() become available in numpy? It might have always been there, but I want to double-check.
There was a problem hiding this comment.
On 09 Jul 2015, at 17:16, Benjamin Root notifications@github.com wrote:
if im.is_grayscale:
r, g, b = rgb.astype(np.float32).transpose(2, 0, 1)just as a sanity check, when did this form of transpose() become available in numpy? It might have always been there, but I want to double-check.
Not sure when exactly, but it’s documented in the 2006 copy of numpybook.pdf, which mentions version 1.0.2.dev3478. We support numpy 1.6 and up. One of the Travis builders uses Python 2.6 with numpy 1.6, and this code does get exercised by some of the tests, as evidenced by the tests failing before I made the two last commits to make the code compatible with Python 2.6.
There was a problem hiding this comment.
Ah, didn't realize one of our Travis instances was using np1.6. Carry on.
ENH: Use png predictors when compressing images in pdf files
There was a problem hiding this comment.
This line is now causing failures in python2.6 and python2.7 image tests on master. The error message says that the buffer does not have a method "readinto()".
There was a problem hiding this comment.
This should reduce pdf file sizes when they include large raster images.
This code is failing at least the test I recently added to check that the alpha channel is output for grayscale images, so there must be some bug lurking in there.