Skip to content

Commit 2c0f3bb

Browse files
authored
ENH: Add support for WebP (#1111)
1 parent 4abd070 commit 2c0f3bb

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

doc/advanced.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ Image scrapers are functions (or callable class instances) that do the following
154154
things:
155155

156156
1. Collect a list of images created in the latest execution of code.
157-
2. Write these images to disk in PNG, JPEG, SVG, or GIF format (with .png,
158-
.jpg, .svg, or .gif extensions, respectively)
157+
2. Write these images to disk in PNG, JPEG, SVG, GIF, or WebP format (with .png,
158+
.jpg, .svg, .gif, or .webp extensions, respectively)
159159
3. Return reST that embeds these figures in the built documentation.
160160

161161
The function should take the following inputs (in this order):
@@ -197,8 +197,8 @@ reST (see below).
197197

198198
This function will be called once for each code block of your examples.
199199
Sphinx-Gallery will take care of scaling images for the gallery
200-
index page thumbnails. PNG images are scaled using Pillow, and
201-
SVG images are copied.
200+
index page thumbnails. PNG, JPEG and WebP images are scaled using Pillow, and
201+
SVG and GIF images are copied.
202202

203203
.. warning:: SVG images do not work with ``latex`` build modes, thus will not
204204
work while building a PDF version of your documentation.
@@ -305,8 +305,8 @@ Example 3: matplotlib with SVG format
305305
-------------------------------------
306306
The :func:`sphinx_gallery.scrapers.matplotlib_scraper` supports ``**kwargs``
307307
to pass to :meth:`matplotlib.figure.Figure.savefig`, one of which is the
308-
``format`` argument. Currently Sphinx-Gallery supports PNG (default) and SVG
309-
output formats. To use SVG, you can do::
308+
``format`` argument. See :ref:`custom_scraper` for supported formats.
309+
To use SVG, you can do::
310310

311311
from sphinx_gallery.scrapers import matplotlib_scraper
312312

sphinx_gallery/scrapers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def matplotlib_scraper(block, block_vars, gallery_conf, **kwargs):
100100
Additional keyword arguments to pass to
101101
:meth:`~matplotlib.figure.Figure.savefig`, e.g. ``format='svg'``.
102102
The ``format`` kwarg in particular is used to set the file extension
103-
of the output file (currently only 'png', 'jpg', and 'svg' are
104-
supported).
103+
of the output file (currently only 'png', 'jpg', 'svg', 'gif', and
104+
'webp' are supported).
105105
106106
Returns
107107
-------
@@ -300,7 +300,8 @@ def __next__(self):
300300

301301

302302
# For now, these are what we support
303-
_KNOWN_IMG_EXTS = ('png', 'svg', 'jpg', 'gif')
303+
# Update advanced.rst if this list is changed
304+
_KNOWN_IMG_EXTS = ('png', 'svg', 'jpg', 'gif', 'webp')
304305

305306

306307
def _find_image_ext(path):

sphinx_gallery/tests/test_full.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
# total number of plot_*.py files in tinybuild/examples + examples_rst_index
3232
# + examples_with_rst
33-
N_EXAMPLES = 14 + 3 + 2
33+
N_EXAMPLES = 15 + 3 + 2
3434
N_FAILING = 2
3535
N_GOOD = N_EXAMPLES - N_FAILING # galleries that run w/o error
3636
# passthroughs examples_rst_index, examples_with_rst
@@ -289,6 +289,7 @@ def test_image_formats(sphinx_app):
289289
thumb_fnames = ['../_images/sphx_glr_plot_svg_thumb.svg',
290290
'../_images/sphx_glr_plot_numpy_matplotlib_thumb.png',
291291
'../_images/sphx_glr_plot_animation_thumb.gif',
292+
'../_images/sphx_glr_plot_webp_thumb.webp',
292293
]
293294
for thumb_fname in thumb_fnames:
294295
file_fname = op.join(generated_examples_dir, thumb_fname)
@@ -300,7 +301,8 @@ def test_image_formats(sphinx_app):
300301
for ex, ext, nums, extra in (
301302
('plot_svg', 'svg', [1], None),
302303
('plot_numpy_matplotlib', 'png', [1], None),
303-
('plot_animation', 'png', [1, 3], 'function Animation')):
304+
('plot_animation', 'png', [1, 3], 'function Animation'),
305+
('plot_webp', 'webp', [1], None)):
304306
html_fname = op.join(generated_examples_dir, '%s.html' % ex)
305307
with codecs.open(html_fname, 'r', 'utf-8') as fid:
306308
html = fid.read()
@@ -314,7 +316,8 @@ def test_image_formats(sphinx_app):
314316
(ex, num, ext))
315317
file_fname2 = op.join(generated_examples_dir, img_fname2)
316318
want_html = f'srcset="{img_fname0}, {img_fname2} 2.00x"'
317-
if ext in ('png', 'jpg', 'svg'): # check 2.00x (tests directive)
319+
if ext in ('png', 'jpg', 'svg', 'webp'):
320+
# check 2.00x (tests directive)
318321
assert op.isfile(file_fname2), file_fname2
319322
assert want_html in html
320323

sphinx_gallery/tests/tinybuild/doc/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def __call__(self, block, block_vars, gallery_conf):
1313
if op.basename(block_vars['target_file']) == 'plot_svg.py' and \
1414
gallery_conf['builder_name'] != 'latex':
1515
kwargs['format'] = 'svg'
16+
elif op.basename(block_vars['target_file']) == 'plot_webp.py' and \
17+
gallery_conf['builder_name'] != 'latex':
18+
kwargs['format'] = 'webp'
1619
return matplotlib_scraper(block, block_vars, gallery_conf, **kwargs)
1720

1821

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
============
3+
Save as WebP
4+
============
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
9+
plt.plot([1, 2])

0 commit comments

Comments
 (0)