def test_save_mayavi_figures(gallery_conf, req_mpl, req_pil): """Test file naming when saving figures. Requires mayavi.""" Image = _get_image() try: from mayavi import mlab except ImportError: raise pytest.skip('Mayavi not installed') import matplotlib.pyplot as plt mlab.options.offscreen = True gallery_conf.update( image_scrapers=(matplotlib_scraper, mayavi_scraper)) fname_template = os.path.join(gallery_conf['gallery_dir'], 'image{0}.png') image_path_iterator = ImagePathIterator(fname_template) block = ('',) * 3 block_vars = dict(image_path_iterator=image_path_iterator) plt.axes([-0.1, -0.1, 1.2, 1.2]) plt.pcolor([[0]], cmap='Greens') mlab.test_plot3d() image_rst = save_figures(block, block_vars, gallery_conf) assert len(plt.get_fignums()) == 0 assert len(image_path_iterator) == 2 assert '/image0.png' not in image_rst assert '/image1.png' in image_rst assert '/image2.png' in image_rst assert '/image3.png' not in image_rst assert not os.path.isfile(fname_template.format(0)) assert os.path.isfile(fname_template.format(1)) assert os.path.isfile(fname_template.format(2)) assert not os.path.isfile(fname_template.format(0)) with Image.open(fname_template.format(1)) as img: pixels = np.asarray(img.convert("RGB")) assert (pixels == [247, 252, 245]).all() # plt first # Test next-value handling, plus image_scrapers modification gallery_conf.update(image_scrapers=(matplotlib_scraper,)) mlab.test_plot3d() plt.axes([-0.1, -0.1, 1.2, 1.2]) plt.pcolor([[0]], cmap='Reds') image_rst = save_figures(block, block_vars, gallery_conf) assert len(plt.get_fignums()) == 0 assert len(image_path_iterator) == 3 assert '/image1.png' not in image_rst assert '/image2.png' not in image_rst assert '/image3.png' in image_rst assert '/image4.png' not in image_rst assert not os.path.isfile(fname_template.format(0)) for ii in range(3): assert os.path.isfile(fname_template.format(ii + 1)) assert not os.path.isfile(fname_template.format(4)) with Image.open(fname_template.format(3)) as img: pixels = np.asarray(img.convert("RGB")) assert (pixels == [255, 245, 240]).all()
def test_thumbnail_path(sphinx_app, tmpdir): """Test sphinx_gallery_thumbnail_path.""" # Make sure our thumbnail matches what it should be fname_orig = op.join(sphinx_app.srcdir, '_static', 'demo.png') fname_thumb = op.join(sphinx_app.outdir, '_images', 'sphx_glr_plot_second_future_imports_thumb.png') fname_new = str(tmpdir.join('new.png')) scale_image(fname_orig, fname_new, *sphinx_app.config.sphinx_gallery_conf["thumbnail_size"]) Image = _get_image() orig = np.asarray(Image.open(fname_thumb)) new = np.asarray(Image.open(fname_new)) assert new.shape == orig.shape corr = np.corrcoef(new.ravel(), orig.ravel())[0, 1] assert corr > 0.99
def test_thumbnail_path(sphinx_app, tmpdir): """Test sphinx_gallery_thumbnail_path.""" import numpy as np # Make sure our thumbnail matches what it should be fname_orig = op.join(sphinx_app.srcdir, '_static_nonstandard', 'demo.png') fname_thumb = op.join(sphinx_app.outdir, '_images', 'sphx_glr_plot_second_future_imports_thumb.png') fname_new = str(tmpdir.join('new.png')) scale_image(fname_orig, fname_new, *sphinx_app.config.sphinx_gallery_conf["thumbnail_size"]) Image = _get_image() orig = np.asarray(Image.open(fname_thumb)) new = np.asarray(Image.open(fname_new)) assert new.shape[:2] == orig.shape[:2] assert new.shape[2] in (3, 4) # optipng can strip the alpha channel corr = np.corrcoef(new[..., :3].ravel(), orig[..., :3].ravel())[0, 1] assert corr > 0.99
def test_negative_thumbnail_config(sphinx_app, tmpdir): """Test 'sphinx_gallery_thumbnail_number' config works correctly for negative numbers.""" import numpy as np # Make sure our thumbnail is the 2nd (last) image fname_orig = op.join(sphinx_app.outdir, '_images', 'sphx_glr_plot_matplotlib_alt_002.png') fname_thumb = op.join(sphinx_app.outdir, '_images', 'sphx_glr_plot_matplotlib_alt_thumb.png') fname_new = str(tmpdir.join('new.png')) scale_image(fname_orig, fname_new, *sphinx_app.config.sphinx_gallery_conf["thumbnail_size"]) Image = _get_image() orig = np.asarray(Image.open(fname_thumb)) new = np.asarray(Image.open(fname_new)) assert new.shape[:2] == orig.shape[:2] assert new.shape[2] in (3, 4) # optipng can strip the alpha channel corr = np.corrcoef(new[..., :3].ravel(), orig[..., :3].ravel())[0, 1] assert corr > 0.99
def req_pil(): try: _get_image() except ExtensionError: pytest.skip('Test requires pillow')
def req_pil(): try: _get_image() except RuntimeError: pytest.skip('Test requires pillow')