Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a3cb52

Browse files
author
Daniel Hyams
committed
Added jpeg quality support for wx backend
1 parent 05fc06d commit 0a3cb52

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/matplotlib/backends/backend_wx.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,15 +1162,29 @@ def _print_image(self, filename, filetype, *args, **kwargs):
11621162
gc = renderer.new_gc()
11631163

11641164
self.figure.draw(renderer)
1165+
1166+
# image is the object that we call SaveFile on.
1167+
image = self.bitmap
1168+
1169+
# set the JPEG quality appropriately. Unfortunately, it is only possible
1170+
# to set the quality on a wx.Image object. So if we are saving a JPEG,
1171+
# convert the wx.Bitmap to a wx.Image, and set the quality.
1172+
if filetype == wx.BITMAP_TYPE_JPEG:
1173+
jpeg_quality = kwargs.get('quality',rcParams['savefig.jpeg_quality'])
1174+
image = self.bitmap.ConvertToImage()
1175+
image.SetOption(wx.IMAGE_OPTION_QUALITY,jpeg_quality)
1176+
11651177

11661178
# Now that we have rendered into the bitmap, save it
11671179
# to the appropriate file type and clean up
11681180
if is_string_like(filename):
1169-
if not self.bitmap.SaveFile(filename, filetype):
1181+
if not image.SaveFile(filename, filetype):
11701182
DEBUG_MSG('print_figure() file save error', 4, self)
11711183
raise RuntimeError('Could not save figure to %s\n' % (filename))
11721184
elif is_writable_file_like(filename):
1173-
if not self.bitmap.ConvertToImage().SaveStream(filename, filetype):
1185+
if not isinstance(image,wx.Image):
1186+
image = image.ConvertToImage()
1187+
if not image.SaveStream(filename, filetype):
11741188
DEBUG_MSG('print_figure() file save error', 4, self)
11751189
raise RuntimeError('Could not save figure to %s\n' % (filename))
11761190

0 commit comments

Comments
 (0)