|
15 | 15 | from matplotlib.image import (BboxImage, imread, NonUniformImage,
|
16 | 16 | AxesImage, FigureImage, PcolorImage)
|
17 | 17 | from matplotlib.transforms import Bbox, Affine2D, TransformedBbox
|
18 |
| -from matplotlib import rcParams |
| 18 | +from matplotlib import rcParams, rc_context |
19 | 19 | from matplotlib import patches
|
20 | 20 | import matplotlib.pyplot as plt
|
21 | 21 |
|
|
35 | 35 |
|
36 | 36 | try:
|
37 | 37 | from PIL import Image
|
38 |
| - del Image |
39 | 38 | HAS_PIL = True
|
40 | 39 | except ImportError:
|
41 | 40 | HAS_PIL = False
|
@@ -494,6 +493,34 @@ def test_nonuniformimage_setnorm():
|
494 | 493 | im.set_norm(plt.Normalize())
|
495 | 494 |
|
496 | 495 |
|
| 496 | +@knownfailureif(not HAS_PIL) |
| 497 | +@cleanup |
| 498 | +def test_jpeg_alpha(): |
| 499 | + plt.figure(figsize=(1, 1), dpi=300) |
| 500 | + # Create an image that is all black, with a gradient from 0-1 in |
| 501 | + # the alpha channel from left to right. |
| 502 | + im = np.zeros((300, 300, 4), dtype=float) |
| 503 | + im[..., 3] = np.linspace(0.0, 1.0, 300) |
| 504 | + |
| 505 | + plt.figimage(im) |
| 506 | + |
| 507 | + buff = io.BytesIO() |
| 508 | + with rc_context({'savefig.facecolor': 'red'}): |
| 509 | + plt.savefig(buff, transparent=True, format='jpg', dpi=300) |
| 510 | + |
| 511 | + buff.seek(0) |
| 512 | + image = Image.open(buff) |
| 513 | + |
| 514 | + # If this fails, there will be only one color (all black). If this |
| 515 | + # is working, we should have all 256 shades of grey represented. |
| 516 | + num_colors = len(image.getcolors(256)) |
| 517 | + assert 175 <= num_colors <= 185, 'num colors: %d' % (num_colors, ) |
| 518 | + # The fully transparent part should be red, not white or black |
| 519 | + # or anything else |
| 520 | + corner_pixel = image.getpixel((0, 0)) |
| 521 | + assert corner_pixel == (254, 0, 0), "corner pixel: %r" % (corner_pixel, ) |
| 522 | + |
| 523 | + |
497 | 524 | @cleanup
|
498 | 525 | def test_nonuniformimage_setdata():
|
499 | 526 | ax = plt.gca()
|
|
0 commit comments