Skip to content

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

Merged
timhoffm merged 1 commit into
matplotlib:masterfrom
anntzer:jpeg
Sep 11, 2019
Merged

timhoffm merged 1 commit into
matplotlib:masterfrom
anntzer:jpeg

Conversation

@anntzer

@anntzer anntzer commented Sep 9, 2019

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Member

Does this need a change note?

@anntzer

anntzer commented Sep 11, 2019

Copy link
Copy Markdown
Contributor Author

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

@timhoffm

timhoffm commented Sep 11, 2019 •

Copy link
Copy Markdown
Member

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