@@ -551,14 +551,15 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,
551
551
`PIL.Image.Image.save` when saving the figure. These take
552
552
precedence over *quality*, *optimize* and *progressive*.
553
553
"""
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 ))
555
561
if dryrun :
556
562
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 )
562
563
if pil_kwargs is None :
563
564
pil_kwargs = {}
564
565
for k in ["quality" , "optimize" , "progressive" ]:
@@ -575,8 +576,9 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,
575
576
"quality will be 75, matching the default of Pillow and "
576
577
"libjpeg." )
577
578
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 ))
580
582
581
583
print_jpeg = print_jpg
582
584
0 commit comments