Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit60466fb

Browse files
authored
Merge pull request#21274 from kinshukdua/master
ENH: Add support to save images in WebP format
2 parents5f4a8ad +394632c commit60466fb

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,28 @@ def print_tif(self, filename_or_obj, *, pil_kwargs=None):
596596

597597
print_tiff=print_tif
598598

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

600622
@_Backend.export
601623
class_BackendAgg(_Backend):

‎lib/matplotlib/tests/test_agg.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,25 @@ def test_pil_kwargs_tiff():
249249
asserttags["ImageDescription"]=="test image"
250250

251251

252+
deftest_pil_kwargs_webp():
253+
plt.plot([0,1,2], [0,1,0])
254+
buf_small=io.BytesIO()
255+
pil_kwargs_low= {"quality":1}
256+
plt.savefig(buf_small,format="webp",pil_kwargs=pil_kwargs_low)
257+
buf_large=io.BytesIO()
258+
pil_kwargs_high= {"quality":100}
259+
plt.savefig(buf_large,format="webp",pil_kwargs=pil_kwargs_high)
260+
assertbuf_large.getbuffer().nbytes>buf_small.getbuffer().nbytes
261+
262+
263+
deftest_webp_alpha():
264+
plt.plot([0,1,2], [0,1,0])
265+
buf=io.BytesIO()
266+
plt.savefig(buf,format="webp",transparent=True)
267+
im=Image.open(buf)
268+
assertim.mode=="RGBA"
269+
270+
252271
deftest_draw_path_collection_error_handling():
253272
fig,ax=plt.subplots()
254273
ax.scatter([1], [1]).set_paths(path.Path([(0,1), (2,3)]))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp