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

Skip to content

Commit 4d12580

Browse files
committed
Get background color from savefig.facecolor
1 parent bdea63d commit 4d12580

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from matplotlib.mathtext import MathTextParser
3939
from matplotlib.path import Path
4040
from matplotlib.transforms import Bbox, BboxBase
41+
from matplotlib import colors as mcolors
4142

4243
from matplotlib.backends._backend_agg import RendererAgg as _RendererAgg
4344
from matplotlib import _png
@@ -575,7 +576,9 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
575576
# The image is "pasted" onto a white background image to safely
576577
# handle any transparency
577578
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
578-
background = Image.new('RGB', size, (255, 255, 255))
579+
color = mcolors.colorConverter.to_rgb(
580+
rcParams.get('savefig.facecolor', 'white'))
581+
background = Image.new('RGB', size, color)
579582
background.paste(image, image)
580583
options = restrict_dict(kwargs, ['quality', 'optimize',
581584
'progressive'])

lib/matplotlib/tests/test_image.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
99
from matplotlib.image import BboxImage, imread, NonUniformImage
1010
from matplotlib.transforms import Bbox
11-
from matplotlib import rcParams
11+
from matplotlib import rcParams, rc_context
1212
import matplotlib.pyplot as plt
1313
from nose.tools import assert_raises
1414
from numpy.testing import assert_array_equal, assert_array_almost_equal
@@ -465,14 +465,18 @@ def test_jpeg_alpha():
465465
plt.figimage(im)
466466

467467
buff = io.BytesIO()
468-
plt.savefig(buff, transparent=True, format='jpg', dpi=300)
468+
with rc_context({'savefig.facecolor': 'red'}):
469+
plt.savefig(buff, transparent=True, format='jpg', dpi=300)
469470

470471
buff.seek(0)
471472
image = Image.open(buff)
472473

473474
# If this fails, there will be only one color (all black). If this
474475
# is working, we should have all 256 shades of grey represented.
475476
assert len(image.getcolors(256)) == 256
477+
# The fully transparent part should be red, not white or black
478+
# or anything else
479+
assert image.getpixel(0, 0) == (0, 0, 255)
476480

477481

478482
if __name__=='__main__':

0 commit comments

Comments
 (0)