Reuse png metadata handling of imsave() in FigureCanvasAgg.print_png(). by anntzer · Pull Request #15435 · matplotlib/matplotlib · GitHub
Skip to content

Reuse png metadata handling of imsave() in FigureCanvasAgg.print_png(). - #15435

Merged
QuLogic merged 4 commits into
matplotlib:masterfrom
anntzer:reuse-imsave
May 29, 2020
Merged

QuLogic merged 4 commits into
matplotlib:masterfrom
anntzer:reuse-imsave

Conversation

@anntzer

@anntzer anntzer commented Oct 17, 2019

Copy link
Copy Markdown
Contributor

This avoids duplicating the conversion of metadata to PngInfo and
revealed a bug in the priority between metadata and pil_kwargs in
imsave(). (That bug is also fixed for 3.2 in #15434; this PR effectively
tests that.)

Note that because np.asarray(self.buffer_rgba()) is already a RGBA
uint8 array, there is no colormapping step happening in imsave().

Ideally mplcairo should also be able to use imsave() for saving to png.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer

anntzer commented Oct 18, 2019

Copy link
Copy Markdown
Contributor Author

@timhoffm timhoffm left a comment

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 is a rather big change just for removing the duplication of metadata and pil_kwargs merging. In particular, replacing a simple Image.save() with the more general mpl.image.imsave()feels a bit risky and obscures the actual quite simple call (I didn't easily manage to follow if imsave() with the given parameters is equivalent to the original code).

Extracting a _merge_metadata_pil_kwargs() function seems to be a simpler solution. - Or did I miss any other important stuff that imsave() is now doing?

@anntzer

anntzer commented Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

The calls are equivalent, although I readily admit that it's not obvious to follow them (the relevant point is at

and which just returns the RGBA array as is).

As suggested above one of the reasons to move the pil_kwargs handling logic in imsave() is to let mplcairo also take advantage of it (well, if I need to call private APIs in mplcairo I can do it too :) but I'd rather not).

@anntzer anntzer added this to the v3.3.0 milestone Jan 15, 2020
@anntzer anntzer added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jan 15, 2020
@anntzer

anntzer commented Jan 15, 2020

Copy link
Copy Markdown
Contributor Author

Not urgent by any means, but does need to get into 3.3 to prevent a (subtle) regression against 3.2, for which the same issue was fixed in #15434, so tagging as release critical.

@tacaswell

Copy link
Copy Markdown
Member

Would this be fixed by merging 3.2.x into master?

If not this needs a re-base.

@anntzer

anntzer commented Apr 30, 2020

Copy link
Copy Markdown
Contributor Author

rebased

Comment thread lib/matplotlib/backends/backend_agg.py
Comment thread lib/matplotlib/image.py

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.

I could also see a case for raising, but warning seems like a step in the right direction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I left this as is but can raise too, just pick one.

This avoids duplicating the conversion of metadata to PngInfo and
revealed a bug in the priority between `metadata` and `pil_kwargs` in
imsave().

Note that because `np.asarray(self.buffer_rgba())` is already a RGBA
uint8 array, there is no colormapping step happening in imsave().

Ideally mplcairo should also be able to use imsave() for saving to png.
Comment thread lib/matplotlib/image.py Outdated
Comment thread lib/matplotlib/image.py
@QuLogic
QuLogic merged commit da3baa1 into matplotlib:master May 29, 2020
@anntzer
anntzer deleted the reuse-imsave branch May 29, 2020 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants