Skip to content

Composite against white, not the savefig.facecolor rc, in print_jpeg. #15239

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 1 commit into from
Sep 11, 2019

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Sep 9, 2019

jpeg doesn't support alpha transparency, so we need to paste the image
against some solid background before saving it. Pasting against the
savefig.facecolor rc, as the code did before, is just wrong: it makes
e.g.

plot([1, 2])
rcParams["savefig.facecolor"] = "blue"
savefig("/tmp/foo.jpg", facecolor=(1, 0, 0, .5))

result in a purple background (blue + 0.5 red), instead of a red one (as
for png -- the facecolor kwarg should just override the rcParam).
Likewise the test_jpeg_alpha test was just wrong (indeed, trying to run
the same code but saving as png shows that transparent=True should
override the savefig.facecolor rcParam).

Instead, paste the image against a white background, which fixes the
issues (and is tested by the fixed test_jpeg_alpha).

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

jpeg doesn't support alpha transparency, so we need to paste the image
against some solid background before saving it.  Pasting against the
savefig.facecolor rc, as the code did before, is just wrong: it makes
e.g.
```
plot([1, 2])
rcParams["savefig.facecolor"] = "blue"
savefig("/tmp/foo.jpg", facecolor=(1, 0, 0, .5))
```
result in a purple background (blue + 0.5 red), instead of a red one (as
for png -- the facecolor kwarg should just override the rcParam).
Likewise the test_jpeg_alpha test was just wrong (indeed, trying to run
the same code but saving as png shows that transparent=True should
override the savefig.facecolor rcParam).

Instead, paste the image against a white background, which fixes the
issues (and is tested by the fixed test_jpeg_alpha).
@timhoffm
Copy link
Member

Does this need a change note?

@anntzer
Copy link
Contributor Author

anntzer commented Sep 11, 2019

It's more like an obscure bugfix, I guess, and no one ever reported this so I wouldn't bother :p

@timhoffm
Copy link
Member

timhoffm commented Sep 11, 2019

AFAICS this only happens when

  • you explicitly set a non-white rcParams["savefig.facecolor"] and
  • you additionally set a semi-transparent facecolor in savefig()

That's probably a very rare combination. So, I'm fine without a note.

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.

3 participants