Improve handling of alpha when saving to jpeg. by anntzer · Pull Request #15437 · matplotlib/matplotlib · GitHub
Skip to content

Improve handling of alpha when saving to jpeg. - #15437

Merged
QuLogic merged 1 commit into
matplotlib:masterfrom
anntzer:jpegalpha
Mar 24, 2020
Merged

QuLogic merged 1 commit into
matplotlib:masterfrom
anntzer:jpegalpha

Conversation

@anntzer

@anntzer anntzer commented Oct 18, 2019

Copy link
Copy Markdown
Contributor

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

@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.

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?

Comment thread lib/matplotlib/backends/backend_agg.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.

The comment needs to be adapted.

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.

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

@anntzer

anntzer commented Oct 18, 2019

Copy link
Copy Markdown
Contributor Author

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.

Comment thread lib/matplotlib/backends/backend_agg.py Outdated
over *quality*, *optimize* and *progressive*.
"""
FigureCanvasAgg.draw(self)
# The image is "pasted" onto a white background to handle transparency.

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 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

@anntzer anntzer Oct 18, 2019

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.

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

QuLogic commented Mar 23, 2020

Copy link
Copy Markdown
Member

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

@anntzer

anntzer commented Mar 24, 2020

Copy link
Copy Markdown
Contributor Author

I'm not sure why it would?

@QuLogic

QuLogic commented Mar 24, 2020

Copy link
Copy Markdown
Member

@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