|
1 | 1 | from __future__ import print_function |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import tempfile |
| 5 | + |
| 6 | + |
| 7 | +from numpy.testing import assert_array_almost_equal |
| 8 | + |
| 9 | + |
| 10 | +from matplotlib.image import imread |
| 11 | +from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas |
| 12 | +from matplotlib.figure import Figure |
| 13 | +from matplotlib.testing.decorators import cleanup |
| 14 | + |
| 15 | + |
| 16 | +@cleanup |
| 17 | +def test_repeated_save_with_alpha(): |
| 18 | + # We want an image which has a background color of blue, with an |
| 19 | + # alpha of 0.25. |
| 20 | + |
| 21 | + fig = Figure([1, 0.4]) |
| 22 | + canvas = FigureCanvas(fig) |
| 23 | + fig.set_facecolor((0, 0, 0.4)) |
| 24 | + fig.patch.set_alpha(0.25) |
| 25 | + |
| 26 | + # The target color is fig.patch.get_facecolor() |
| 27 | + |
| 28 | + _, img_fname = tempfile.mkstemp(suffix='.png') |
| 29 | + try: |
| 30 | + fig.savefig(img_fname, |
| 31 | + facecolor=fig.get_facecolor(), |
| 32 | + edgecolor='none') |
| 33 | + |
| 34 | + # Save the figure again to check that the |
| 35 | + # colors don't bleed from the previous renderer. |
| 36 | + fig.savefig(img_fname, |
| 37 | + facecolor=fig.get_facecolor(), |
| 38 | + edgecolor='none') |
| 39 | + |
| 40 | + # Check the first pixel has the desired color & alpha |
| 41 | + # (approx: 0, 0, 0.1, 0.25) |
| 42 | + assert_array_almost_equal(tuple(imread(img_fname)[0, 0]), |
| 43 | + (0.0, 0.0, 0.098, 0.250), |
| 44 | + decimal=3) |
| 45 | + finally: |
| 46 | + os.remove(img_fname) |
| 47 | + |
4 | 48 |
|
5 | 49 | def report_memory(i): |
6 | 50 | pid = os.getpid() |
@@ -64,3 +108,8 @@ def report_memory(i): |
64 | 108 | ## # w/o text and w/o write_png: Average memory consumed per loop: 0.02 |
65 | 109 | ## # w/o text and w/ write_png : Average memory consumed per loop: 0.3400 |
66 | 110 | ## # w/ text and w/ write_png : Average memory consumed per loop: 0.32 |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + import nose |
| 115 | + nose.runmodule(argv=['-s', '--with-doctest'], exit=False) |
0 commit comments