Skip to content

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

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

Merged
merged 4 commits into from
May 29, 2020

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Oct 17, 2019

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
Copy link
Contributor Author

anntzer commented Oct 18, 2019

Now also includes the warning on metadata/pnginfo collision suggested in #15434 (comment). This effectively requires moving the generation of the Software tag to imsave (as we otherwise don't know whether the tag was autogenerated or provided by the user).

Copy link
Member

@timhoffm timhoffm left a comment

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
Copy link
Contributor Author

anntzer commented Oct 28, 2019

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
Copy link
Contributor Author

anntzer commented Jan 15, 2020

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
Member

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

If not this needs a re-base.

@anntzer
Copy link
Contributor Author

anntzer commented Apr 30, 2020

rebased

# semantics of duplicate keys in pnginfo is unclear.
if "pnginfo" in pil_kwargs:
if metadata:
cbook._warn_external("'metadata' is overridden by the "
Copy link
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
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.
@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