10000 Fix #5302: Proper alpha-blending for jpeg by mdboom · Pull Request #5324 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix #5302: Proper alpha-blending for jpeg #5324

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 5 commits into from
Oct 28, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get background color from savefig.facecolor
  • Loading branch information
mdboom committed Oct 27, 2015
commit 3dea66d9e591cefe551214349abb3c12516c5a92
6 changes: 5 additions & 1 deletion lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from matplotlib.mathtext import MathTextParser
from matplotlib.path import Path
from matplotlib.transforms import Bbox, BboxBase
from matplotlib import colors as mcolors

from matplotlib.backends._backend_agg import RendererAgg as _RendererAgg
from matplotlib import _png
Expand Down Expand Up @@ -575,7 +576,10 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
# The image is "pasted" onto a white background image to safely
# handle any transparency
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
background = Image.new('RGB', size, (255, 255, 255))
color = mcolors.colorConverter.to_rgb(
rcParams.get('savefig.facecolor', 'white'))
color = tuple([int(x * 255.0) for x in color])
background = Image.new('RGB', size, color)
background.paste(image, image)
options = restrict_dict(kwargs, ['quality', 'optimize',
'progressive'])
Expand Down
10 changes: 7 additions & 3 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
from matplotlib.image import BboxImage, imread, NonUniformImage
from matplotlib.transforms import Bbox
from matplotlib import rcParams
from matplotlib import rcParams, rc_context
import matplotlib.pyplot as plt
from nose.tools import assert_raises
from numpy.testing import assert_array_equal, assert_array_almost_equal
Expand Down Expand Up @@ -465,14 +465,18 @@ def test_jpeg_alpha():
plt.figimage(im)

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

buff.seek(0)
image = Image.open(buff)

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


if __name__=='__main__':
Expand Down
0