@@ -551,14 +551,15 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,
551551 `PIL.Image.Image.save` when saving the figure. These take
552552 precedence over *quality*, *optimize* and *progressive*.
553553 """
554- FigureCanvasAgg .draw (self )
554+ # Remove transparency by alpha-blending on an assumed white background.
555+ r , g , b , a = mcolors .to_rgba (self .figure .get_facecolor ())
556+ try :
557+ self .figure .set_facecolor (a * np .array ([r , g , b ]) + 1 - a )
558+ FigureCanvasAgg .draw (self )
559+ finally :
560+ self .figure .set_facecolor ((r , g , b , a ))
555561 if dryrun :
556562 return
557- # The image is "pasted" onto a white background image to safely
558- # handle any transparency
559- image = Image .fromarray (np .asarray (self .buffer_rgba ()))
560- background = Image .new ("RGB" , image .size , "white" )
561- background .paste (image , image )
562563 if pil_kwargs is None :
563564 pil_kwargs = {}
564565 for k in ["quality" , "optimize" , "progressive" ]:
@@ -575,8 +576,9 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,
575576 "quality will be 75, matching the default of Pillow and "
576577 "libjpeg." )
577578 pil_kwargs .setdefault ("dpi" , (self .figure .dpi , self .figure .dpi ))
578- return background .save (
579- filename_or_obj , format = 'jpeg' , ** pil_kwargs )
579+ # Drop alpha channel now.
580+ return (Image .fromarray (np .asarray (self .buffer_rgba ())[..., :3 ])
581+ .save (filename_or_obj , format = 'jpeg' , ** pil_kwargs ))
580582
581583 print_jpeg = print_jpg
582584
0 commit comments