Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 145984f

Browse files
committed
Add support to save images in WebP format
1 parent 0bb36e8 commit 145984f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
'svgz': 'Scalable Vector Graphics',
7070
'tif': 'Tagged Image File Format',
7171
'tiff': 'Tagged Image File Format',
72+
'webp': 'WebP Image Format',
7273
}
7374
_default_backends = {
7475
'eps': 'matplotlib.backends.backend_ps',
@@ -84,6 +85,7 @@
8485
'svgz': 'matplotlib.backends.backend_svg',
8586
'tif': 'matplotlib.backends.backend_agg',
8687
'tiff': 'matplotlib.backends.backend_agg',
88+
'webp': 'matplotlib.backends.backend_agg',
8789
}
8890

8991

lib/matplotlib/backends/backend_agg.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,29 @@ def print_tif(self, filename_or_obj, *, pil_kwargs=None):
594594

595595
print_tiff = print_tif
596596

597+
@_check_savefig_extra_args
598+
def print_webp(self, filename_or_obj, *, pil_kwargs=None):
599+
"""
600+
Write the figure to a WebP file.
601+
602+
Parameters
603+
----------
604+
filename_or_obj : str or path-like or file-like
605+
The file to write to.
606+
607+
Other Parameters
608+
----------------
609+
pil_kwargs : dict, optional
610+
Additional keyword arguments that are passed to
611+
`PIL.Image.Image.save` when saving the figure.
612+
"""
613+
FigureCanvasAgg.draw(self)
614+
if pil_kwargs is None:
615+
pil_kwargs = {}
616+
pil_kwargs.setdefault("dpi", (self.figure.dpi, self.figure.dpi))
617+
return (Image.fromarray(np.asarray(self.buffer_rgba()))
618+
.save(filename_or_obj, format='webp', **pil_kwargs))
619+
597620

598621
@_Backend.export
599622
class _BackendAgg(_Backend):

0 commit comments

Comments
 (0)