Skip to content

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Oct 18, 2019

We can compose the figure facecolor against a white background before
rendering the image, rather than after. This saves an image composition
step.

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

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.

Temporarily changing the figure background and manually calculating the alpha-shading towards white is more low-level. It's harder to understand what's going on and it might be more susceptible of introducing bugs in the future.

Is the performance gain worth it?

@@ -563,23 +563,26 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,
`PIL.Image.save` when saving the figure. These take precedence
over *quality*, *optimize* and *progressive*.
"""
FigureCanvasAgg.draw(self)
if dryrun:
return
# The image is "pasted" onto a white background image to safely
Copy link
Member

Choose a reason for hiding this comment

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

The comment needs to be adapted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well, this is still pasting the figure over a white background.

@anntzer
Copy link
Contributor Author

anntzer commented Oct 18, 2019

Actually, another motivation for the change is to make buffer_rgba() return an array that can be directly passed to imsave() -- essentially doing the same thing as in #15435, but for jpeg.
Also, I think using more mpl/numpy APIs and fewer Pillow APIs (Image.paste) is actually clearer, not lower-level.

@@ -563,23 +563,25 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,
`PIL.Image.save` when saving the figure. These take precedence
over *quality*, *optimize* and *progressive*.
"""
FigureCanvasAgg.draw(self)
# The image is "pasted" onto a white background to handle transparency.
Copy link
Member

Choose a reason for hiding this comment

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

I think the term "paste" is a bit misleading

Suggested change
# The image is "pasted" onto a white background to handle transparency.
# Remove transparency by alpha-blending on an assumed white background

Copy link
Contributor Author

@anntzer anntzer Oct 18, 2019

Choose a reason for hiding this comment

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

sure, sounds good, edited accordingly

We can compose the figure facecolor against a white background before
rendering the image, rather than after.  This saves an image composition
step.
@QuLogic
Copy link
Member

QuLogic commented Mar 23, 2020

Will this cause weird flickering on interactive views (maybe not regular figures that composite on white, but embedded/notebook/something else)?

@anntzer
Copy link
Contributor Author

anntzer commented Mar 24, 2020

I'm not sure why it would?

@QuLogic
Copy link
Member

QuLogic commented Mar 24, 2020

I was thinking of stuff like changing the DPI, but I see setting the canvas manager is handled in the call stack above this one.

@QuLogic QuLogic merged commit bba7763 into matplotlib:master Mar 24, 2020
@anntzer anntzer deleted the jpegalpha branch March 24, 2020 20:03
@QuLogic QuLogic added this to the v3.3.0 milestone Mar 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants