Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jpeg quality 95 by default with rendering with PIL #1771

Merged
merged 10 commits into from Apr 23, 2013
11 changes: 8 additions & 3 deletions lib/matplotlib/backend_bases.py
Expand Up @@ -1930,9 +1930,10 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
Supported kwargs:

*quality*: The image quality, on a scale from 1 (worst) to
95 (best). The default is 75. Values above 95 should
be avoided; 100 completely disables the JPEG
quantization stage.
95 (best). The default is 95, if not given in the
matplotlibrc file in the savefig.jpeg_quality parameter.
Values above 95 should be avoided; 100 completely
disables the JPEG quantization stage.

*optimize*: If present, indicates that the encoder should
make an extra pass over the image in order to select
Expand All @@ -1949,6 +1950,10 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
options = cbook.restrict_dict(kwargs, ['quality', 'optimize',
'progressive'])

if 'quality' not in options:
options['quality'] = rcParams['savefig.jpeg_quality']

return image.save(filename_or_obj, format='jpeg', **options)
print_jpeg = print_jpg

Expand Down
12 changes: 11 additions & 1 deletion lib/matplotlib/backends/backend_gdk.py
Expand Up @@ -19,6 +19,7 @@ def fn_name(): return sys._getframe(1).f_code.co_name
import numpy as np

import matplotlib
from matplotlib import rcParams
from matplotlib._pylab_helpers import Gcf
from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \
FigureManagerBase, FigureCanvasBase
Expand Down Expand Up @@ -471,4 +472,13 @@ def _print_image(self, filename, format, *args, **kwargs):
pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(),
0, 0, 0, 0, width, height)

pixbuf.save(filename, format)
# set the default quality, if we are writing a JPEG.
# http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--save
options = cbook.restrict_dict(kwargs, ['quality'])
if format in ['jpg','jpeg']:
if 'quality' not in options:
options['quality'] = rcParams['savefig.jpeg_quality']
options['quality'] = str(options['quality'])

pixbuf.save(filename, format, options=options)

16 changes: 13 additions & 3 deletions lib/matplotlib/backends/backend_gtk.py
Expand Up @@ -452,7 +452,7 @@ def print_jpeg(self, filename, *args, **kwargs):
def print_png(self, filename, *args, **kwargs):
return self._print_image(filename, 'png')

def _print_image(self, filename, format):
def _print_image(self, filename, format, *args, **kwargs):
if self.flags() & gtk.REALIZED == 0:
# for self.window(for pixmap) and has a side effect of altering
# figure width,height (via configure-event?)
Expand All @@ -469,17 +469,27 @@ def _print_image(self, filename, format):
pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(),
0, 0, 0, 0, width, height)

# set the default quality, if we are writing a JPEG.
# http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--save
options = cbook.restrict_dict(kwargs, ['quality'])
if format in ['jpg','jpeg']:
if 'quality' not in options:
options['quality'] = rcParams['savefig.jpeg_quality']

options['quality'] = str(options['quality'])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Please use just one newline between blocks of code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean between lines 477 and 478?


if is_string_like(filename):
try:
pixbuf.save(filename, format)
pixbuf.save(filename, format, options=options)
except gobject.GError as exc:
error_msg_gtk('Save figure failure:\n%s' % (exc,), parent=self)
elif is_writable_file_like(filename):
if hasattr(pixbuf, 'save_to_callback'):
def save_callback(buf, data=None):
data.write(buf)
try:
pixbuf.save_to_callback(save_callback, format, user_data=filename)
pixbuf.save_to_callback(save_callback, format, user_data=filename, options=options)
except gobject.GError as exc:
error_msg_gtk('Save figure failure:\n%s' % (exc,), parent=self)
else:
Expand Down
17 changes: 15 additions & 2 deletions lib/matplotlib/backends/backend_wx.py
Expand Up @@ -1162,15 +1162,28 @@ def _print_image(self, filename, filetype, *args, **kwargs):
gc = renderer.new_gc()

self.figure.draw(renderer)

# image is the object that we call SaveFile on.
image = self.bitmap
# set the JPEG quality appropriately. Unfortunately, it is only possible
# to set the quality on a wx.Image object. So if we are saving a JPEG,
# convert the wx.Bitmap to a wx.Image, and set the quality.
if filetype == wx.BITMAP_TYPE_JPEG:
jpeg_quality = kwargs.get('quality',rcParams['savefig.jpeg_quality'])
image = self.bitmap.ConvertToImage()
image.SetOption(wx.IMAGE_OPTION_QUALITY,str(jpeg_quality))


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean line 1168?

# Now that we have rendered into the bitmap, save it
# to the appropriate file type and clean up
if is_string_like(filename):
if not self.bitmap.SaveFile(filename, filetype):
if not image.SaveFile(filename, filetype):
DEBUG_MSG('print_figure() file save error', 4, self)
raise RuntimeError('Could not save figure to %s\n' % (filename))
elif is_writable_file_like(filename):
if not self.bitmap.ConvertToImage().SaveStream(filename, filetype):
if not isinstance(image,wx.Image):
image = image.ConvertToImage()
if not image.SaveStream(filename, filetype):
DEBUG_MSG('print_figure() file save error', 4, self)
raise RuntimeError('Could not save figure to %s\n' % (filename))

Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Expand Up @@ -687,6 +687,7 @@ def __call__(self, s):
'savefig.frameon': [True, validate_bool],
'savefig.orientation': ['portrait', validate_orientation], # edgecolor;
#white
'savefig.jpeg_quality': [95, validate_int],
# what to add to extensionless filenames
'savefig.extension': ['png', deprecate_savefig_extension],
# value checked by backend at runtime
Expand Down
17 changes: 9 additions & 8 deletions matplotlibrc.template
Expand Up @@ -362,14 +362,15 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
# the default savefig params can be different from the display params
# e.g., you may want a higher resolution, or to make the figure
# background white
#savefig.dpi : 100 # figure dots per inch
#savefig.facecolor : white # figure facecolor when saving
#savefig.edgecolor : white # figure edgecolor when saving
#savefig.format : png # png, ps, pdf, svg
#savefig.bbox : standard # 'tight' or 'standard'.
#savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'
#savefig.directory : ~ # default directory in savefig dialog box,
# leave empty to always use current working directory
#savefig.dpi : 100 # figure dots per inch
#savefig.facecolor : white # figure facecolor when saving
#savefig.edgecolor : white # figure edgecolor when saving
#savefig.format : png # png, ps, pdf, svg
#savefig.bbox : standard # 'tight' or 'standard'.
#savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'
#savefig.jpeg_quality: 95 # when a jpeg is saved, the default quality parameter.
#savefig.directory : ~ # default directory in savefig dialog box,
# leave empty to always use current working directory

# tk backend params
#tk.window_focus : False # Maintain shell focus for TkAgg
Expand Down
3 changes: 2 additions & 1 deletion src/ft2font.cpp
Expand Up @@ -1627,7 +1627,8 @@ FT2Font::get_glyph_name(const Py::Tuple & args)
{
throw Py::RuntimeError("Could not get glyph names.");
}
}
}

return Py::String(buffer);
}
PYCXX_VARARGS_METHOD_DECL(FT2Font, get_glyph_name)
Expand Down