@@ -450,6 +450,7 @@ def print_raw(self, filename_or_obj, *args, **kwargs):
450
450
with cbook ._setattr_cm (renderer , dpi = self .figure .dpi ), \
451
451
cbook .open_file_cm (filename_or_obj , "wb" ) as fh :
452
452
fh .write (renderer ._renderer .buffer_rgba ())
453
+
453
454
print_rgba = print_raw
454
455
455
456
def print_png (self , filename_or_obj , * args , ** kwargs ):
@@ -507,7 +508,7 @@ def print_png(self, filename_or_obj, *args, **kwargs):
507
508
with cbook ._setattr_cm (renderer , dpi = self .figure .dpi ), \
508
509
cbook .open_file_cm (filename_or_obj , "wb" ) as fh :
509
510
_png .write_png (renderer ._renderer , fh ,
510
- self .figure .dpi , metadata = metadata )
511
+ self .figure .dpi , metadata = metadata )
511
512
512
513
def print_to_buffer (self ):
513
514
FigureCanvasAgg .draw (self )
@@ -517,8 +518,13 @@ def print_to_buffer(self):
517
518
(int (renderer .width ), int (renderer .height )))
518
519
519
520
if _has_pil :
520
- # add JPEG support
521
- def print_jpg (self , filename_or_obj , * args , dryrun = False , ** kwargs ):
521
+
522
+ # Note that these methods should typically be called via savefig() and
523
+ # print_figure(), and the latter ensures that `self.figure.dpi` already
524
+ # matches the dpi kwarg (if any).
525
+
526
+ def print_jpg (self , filename_or_obj , * args , dryrun = False ,
527
+ pil_kwargs = None , ** kwargs ):
522
528
"""
523
529
Write the figure to a JPEG file.
524
530
@@ -543,6 +549,11 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, **kwargs):
543
549
progressive : bool
544
550
If present, indicates that this image
545
551
should be stored as a progressive JPEG file.
552
+
553
+ pil_kwargs : dict, optional
554
+ Additional keyword arguments that are passed to Pillow when
555
+ saving the figure. These take precedence over *quality*,
556
+ *optimize* and *progressive*.
546
557
"""
547
558
buf , size = self .print_to_buffer ()
548
559
if dryrun :
@@ -554,25 +565,29 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, **kwargs):
554
565
color = tuple ([int (x * 255 ) for x in rgba [:3 ]])
555
566
background = Image .new ('RGB' , size , color )
556
567
background .paste (image , image )
557
- options = {k : kwargs [k ]
558
- for k in ['quality' , 'optimize' , 'progressive' , 'dpi' ]
559
- if k in kwargs }
560
- options .setdefault ('quality' , rcParams ['savefig.jpeg_quality' ])
561
- if 'dpi' in options :
562
- # Set the same dpi in both x and y directions
563
- options ['dpi' ] = (options ['dpi' ], options ['dpi' ])
564
-
565
- return background .save (filename_or_obj , format = 'jpeg' , ** options )
568
+ if pil_kwargs is None :
569
+ pil_kwargs = {}
570
+ for k in ["quality" , "optimize" , "progressive" ]:
571
+ if k in kwargs :
572
+ pil_kwargs .setdefault (k , kwargs [k ])
573
+ pil_kwargs .setdefault ("quality" , rcParams ["savefig.jpeg_quality" ])
574
+ pil_kwargs .setdefault ("dpi" , (self .figure .dpi , self .figure .dpi ))
575
+ return background .save (
576
+ filename_or_obj , format = 'jpeg' , ** pil_kwargs )
577
+
566
578
print_jpeg = print_jpg
567
579
568
- # add TIFF support
569
- def print_tif ( self , filename_or_obj , * args , dryrun = False , ** kwargs ):
580
+ def print_tif ( self , filename_or_obj , * args , dryrun = False ,
581
+ pil_kwargs = None , ** kwargs ):
570
582
buf , size = self .print_to_buffer ()
571
583
if dryrun :
572
584
return
573
585
image = Image .frombuffer ('RGBA' , size , buf , 'raw' , 'RGBA' , 0 , 1 )
574
- dpi = (self .figure .dpi , self .figure .dpi )
575
- return image .save (filename_or_obj , format = 'tiff' , dpi = dpi )
586
+ if pil_kwargs is None :
587
+ pil_kwargs = {}
588
+ pil_kwargs .setdefault ("dpi" , (self .figure .dpi , self .figure .dpi ))
589
+ return image .save (filename_or_obj , format = 'tiff' , ** pil_kwargs )
590
+
576
591
print_tiff = print_tif
577
592
578
593
0 commit comments