Skip to content

ENH: Use png predictors when compressing images in pdf files - #4605

Merged
tacaswell merged 8 commits into
matplotlib:masterfrom
jkseppan:png-in-pdf
Jul 22, 2015
Merged

tacaswell merged 8 commits into
matplotlib:masterfrom
jkseppan:png-in-pdf

Conversation

@jkseppan

@jkseppan jkseppan commented Jul 8, 2015

Copy link
Copy Markdown
Member

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.

jkseppan added 2 commits July 8, 2015 18:11
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.
Comment thread lib/matplotlib/backends/backend_pdf.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

when did this function become available in numpy?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

jkseppan added 2 commits July 8, 2015 23:16
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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The bug in my initial version was that I used 'Smask' instead of 'SMask' here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, didn't realize one of our Travis instances was using np1.6. Carry on.

@tacaswell tacaswell modified the milestone: next point release Jul 17, 2015
tacaswell added a commit that referenced this pull request Jul 22, 2015
ENH: Use png predictors when compressing images in pdf files
@tacaswell
tacaswell merged commit 336c1bb into matplotlib:master Jul 22, 2015

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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()".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That is because the merge of the six branch (#4501) changed it from

from io import BytesIO 

to

from matplotlib.externals.six import BytesIO

six.BytesIO is stringIO.stringIO in python 2

while #4603 changed it to use BytesIO all over.

I.e I think the merge in 8fe495a is wrong

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PR coming up

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

╯‵Д′)╯彡┻━┻

@jkseppan
jkseppan deleted the png-in-pdf branch July 22, 2015 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants