Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb5c84d

Browse files
committed
Reuse imsave()'s background-blending code in FigureCanvasAgg.print_jpeg.
We don't need to implement it twice, nor to mark the figure as stale by temporarily switching its facecolor.
1 parent 67e69e9 commit cb5c84d

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,11 @@ def print_to_buffer(self):
560560

561561
@_api.delete_parameter("3.5", "args")
562562
def print_jpg(self, filename_or_obj, *args, pil_kwargs=None):
563-
# Remove transparency by alpha-blending on an assumed white background.
564-
r, g, b, a = mcolors.to_rgba(self.figure.get_facecolor())
565-
try:
566-
self.figure.set_facecolor(a * np.array([r, g, b]) + 1 - a)
563+
# savefig() has already applied savefig.facecolor; we now set it to
564+
# white to make imsave() blend semi-transparent figures against an
565+
# assumed white background.
566+
with mpl.rc_context({"savefig.facecolor": "white"}):
567567
self._print_pil(filename_or_obj, "jpeg", pil_kwargs)
568-
finally:
569-
self.figure.set_facecolor((r, g, b, a))
570568

571569
print_jpeg = print_jpg
572570

0 commit comments

Comments
 (0)