@@ -487,6 +487,16 @@ def print_raw(self, filename_or_obj, *args):
487
487
488
488
print_rgba = print_raw
489
489
490
+ def _print_pil (self ,filename_or_obj ,fmt ,pil_kwargs ,metadata = None ):
491
+ """
492
+ Draw the canvas, then save it using `.image.imsave` (to which
493
+ *pil_kwargs* and *metadata* are forwarded).
494
+ """
495
+ FigureCanvasAgg .draw (self )
496
+ mpl .image .imsave (
497
+ filename_or_obj ,self .buffer_rgba (),format = fmt ,origin = "upper" ,
498
+ dpi = self .figure .dpi ,metadata = metadata ,pil_kwargs = pil_kwargs )
499
+
490
500
@_check_savefig_extra_args
491
501
@_api .delete_parameter ("3.5" ,"args" )
492
502
def print_png (self ,filename_or_obj ,* args ,
@@ -537,10 +547,7 @@ def print_png(self, filename_or_obj, *args,
537
547
If the 'pnginfo' key is present, it completely overrides
538
548
*metadata*, including the default 'Software' key.
539
549
"""
540
- FigureCanvasAgg .draw (self )
541
- mpl .image .imsave (
542
- filename_or_obj ,self .buffer_rgba (),format = "png" ,origin = "upper" ,
543
- dpi = self .figure .dpi ,metadata = metadata ,pil_kwargs = pil_kwargs )
550
+ self ._print_pil (filename_or_obj ,"png" ,pil_kwargs ,metadata )
544
551
545
552
def print_to_buffer (self ):
546
553
FigureCanvasAgg .draw (self )
@@ -555,68 +562,38 @@ def print_to_buffer(self):
555
562
@_check_savefig_extra_args ()
556
563
@_api .delete_parameter ("3.5" ,"args" )
557
564
def print_jpg (self ,filename_or_obj ,* args ,pil_kwargs = None ,** kwargs ):
558
- """
559
- Write the figure to a JPEG file.
560
-
561
- Parameters
562
- ----------
563
- filename_or_obj : str or path-like or file-like
564
- The file to write to.
565
-
566
- Other Parameters
567
- ----------------
568
- pil_kwargs : dict, optional
569
- Additional keyword arguments that are passed to
570
- `PIL.Image.Image.save` when saving the figure.
571
- """
572
565
# Remove transparency by alpha-blending on an assumed white background.
573
566
r ,g ,b ,a = mcolors .to_rgba (self .figure .get_facecolor ())
574
567
try :
575
568
self .figure .set_facecolor (a * np .array ([r ,g ,b ])+ 1 - a )
576
- FigureCanvasAgg . draw ( self )
569
+ self . _print_pil ( filename_or_obj , "jpeg" , pil_kwargs )
577
570
finally :
578
571
self .figure .set_facecolor ((r ,g ,b ,a ))
579
- if pil_kwargs is None :
580
- pil_kwargs = {}
581
- pil_kwargs .setdefault ("dpi" , (self .figure .dpi ,self .figure .dpi ))
582
- # Drop alpha channel now.
583
- return (Image .fromarray (np .asarray (self .buffer_rgba ())[..., :3 ])
584
- .save (filename_or_obj ,format = 'jpeg' ,** pil_kwargs ))
585
572
586
573
print_jpeg = print_jpg
587
574
588
575
@_check_savefig_extra_args
589
576
def print_tif (self ,filename_or_obj ,* ,pil_kwargs = None ):
590
- FigureCanvasAgg .draw (self )
591
- if pil_kwargs is None :
592
- pil_kwargs = {}
593
- pil_kwargs .setdefault ("dpi" , (self .figure .dpi ,self .figure .dpi ))
594
- return (Image .fromarray (np .asarray (self .buffer_rgba ()))
595
- .save (filename_or_obj ,format = 'tiff' ,** pil_kwargs ))
577
+ self ._print_pil (filename_or_obj ,"tiff" ,pil_kwargs )
596
578
597
579
print_tiff = print_tif
598
580
599
581
@_check_savefig_extra_args
600
582
def print_webp (self ,filename_or_obj ,* ,pil_kwargs = None ):
583
+ self ._print_pil (filename_or_obj ,"webp" ,pil_kwargs )
584
+
585
+ print_jpg .__doc__ ,print_tif .__doc__ ,print_webp .__doc__ = map (
601
586
"""
602
- Write the figure to aWebP file.
587
+ Write the figure to a{} file.
603
588
604
589
Parameters
605
590
----------
606
591
filename_or_obj : str or path-like or file-like
607
592
The file to write to.
608
-
609
- Other Parameters
610
- ----------------
611
593
pil_kwargs : dict, optional
612
594
Additional keyword arguments that are passed to
613
595
`PIL.Image.Image.save` when saving the figure.
614
- """
615
- FigureCanvasAgg .draw (self )
616
- if pil_kwargs is None :
617
- pil_kwargs = {}
618
- return (Image .fromarray (np .asarray (self .buffer_rgba ()))
619
- .save (filename_or_obj ,format = 'webp' ,** pil_kwargs ))
596
+ """ .format , ["JPEG" ,"TIFF" ,"WebP" ])
620
597
621
598
622
599
@_Backend .export