|
8 | 8 | from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
|
9 | 9 | from matplotlib.image import BboxImage, imread, NonUniformImage
|
10 | 10 | from matplotlib.transforms import Bbox
|
11 |
| -from matplotlib import rcParams |
| 11 | +from matplotlib import rcParams, rc_context |
12 | 12 | import matplotlib.pyplot as plt
|
13 | 13 | from nose.tools import assert_raises
|
14 | 14 | from numpy.testing import assert_array_equal, assert_array_almost_equal
|
@@ -453,6 +453,34 @@ def test_nonuniformimage_setnorm():
|
453 | 453 | im = NonUniformImage(ax)
|
454 | 454 | im.set_norm(plt.Normalize())
|
455 | 455 |
|
| 456 | +@knownfailureif(not HAS_PIL) |
| 457 | +@cleanup |
| 458 | +def test_jpeg_alpha(): |
| 459 | + plt.figure(figsize=(1, 1), dpi=300) |
| 460 | + # Create an image that is all black, with a gradient from 0-1 in |
| 461 | + # the alpha channel from left to right. |
| 462 | + im = np.zeros((300, 300, 4), dtype=np.float) |
| 463 | + im[..., 3] = np.linspace(0.0, 1.0, 300) |
| 464 | + |
| 465 | + plt.figimage(im) |
| 466 | + |
| 467 | + buff = io.BytesIO() |
| 468 | + with rc_context({'savefig.facecolor': 'red'}): |
| 469 | + plt.savefig(buff, transparent=True, format='jpg', dpi=300) |
| 470 | + |
| 471 | + buff.seek(0) |
| 472 | + image = Image.open(buff) |
| 473 | + |
| 474 | + # If this fails, there will be only one color (all black). If this |
| 475 | + # is working, we should have all 256 shades of grey represented. |
| 476 | + print("num colors: ", len(image.getcolors(256))) |
| 477 | + assert len(image.getcolors(256)) >= 175 and len(image.getcolors(256)) <= 185 |
| 478 | + # The fully transparent part should be red, not white or black |
| 479 | + # or anything else |
| 480 | + print("corner pixel: ", image.getpixel((0, 0))) |
| 481 | + assert image.getpixel((0, 0)) == (254, 0, 0) |
| 482 | + |
| 483 | + |
456 | 484 | if __name__=='__main__':
|
457 | 485 | import nose
|
458 | 486 | nose.runmodule(argv=['-s','--with-doctest'], exit=False)
|
0 commit comments