Show More
@@ -94,8 +94,8 b' def figsize(sizex, sizey):' | |||
|
94 | 94 | matplotlib.rcParams['figure.figsize'] = [sizex, sizey] |
|
95 | 95 | |
|
96 | 96 | |
|
97 | def print_figure(fig, fmt='png'): | |
|
98 | """Convert a figure to svg or png for inline display.""" | |
|
97 | def print_figure(fig, fmt='png', quality=90): | |
|
98 | """Convert a figure to svg, jpg (if PIL is installed) or png for inline display.""" | |
|
99 | 99 | from matplotlib import rcParams |
|
100 | 100 | # When there's an empty figure, we shouldn't return anything, otherwise we |
|
101 | 101 | # get big blank areas in the qt console. |
@@ -110,7 +110,7 b" def print_figure(fig, fmt='png'):" | |||
|
110 | 110 | dpi = dpi * 2 |
|
111 | 111 | fmt = 'png' |
|
112 | 112 | fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight', |
|
113 | facecolor=fc, edgecolor=ec, dpi=dpi) | |
|
113 | facecolor=fc, edgecolor=ec, dpi=dpi, quality=quality) | |
|
114 | 114 | data = bytes_io.getvalue() |
|
115 | 115 | return data |
|
116 | 116 | |
@@ -163,7 +163,7 b' def mpl_runner(safe_execfile):' | |||
|
163 | 163 | return mpl_execfile |
|
164 | 164 | |
|
165 | 165 | |
|
166 | def select_figure_format(shell, fmt): | |
|
166 | def select_figure_format(shell, fmt, quality): | |
|
167 | 167 | """Select figure format for inline backend, can be 'png', 'retina', or 'svg'. |
|
168 | 168 | |
|
169 | 169 | Using this method ensures only one figure format is active at a time. |
@@ -173,10 +173,14 b' def select_figure_format(shell, fmt):' | |||
|
173 | 173 | |
|
174 | 174 | svg_formatter = shell.display_formatter.formatters['image/svg+xml'] |
|
175 | 175 | png_formatter = shell.display_formatter.formatters['image/png'] |
|
176 | jpg_formatter = shell.display_formatter.formatters['image/jpeg'] | |
|
176 | 177 | |
|
177 | 178 | if fmt == 'png': |
|
178 | 179 | svg_formatter.pop(Figure, None) |
|
179 | 180 | png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png')) |
|
181 | elif fmt in ('jpg', 'jpeg'): | |
|
182 | svg_formatter.type_printers.pop(Figure, None) | |
|
183 | jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', quality)) | |
|
180 | 184 | elif fmt in ('png2x', 'retina'): |
|
181 | 185 | svg_formatter.pop(Figure, None) |
|
182 | 186 | png_formatter.for_type(Figure, retina_figure) |
@@ -338,5 +342,5 b' def configure_inline_support(shell, backend):' | |||
|
338 | 342 | del shell._saved_rcParams |
|
339 | 343 | |
|
340 | 344 | # Setup the default figure format |
|
341 | select_figure_format(shell, cfg.figure_format) | |
|
345 | select_figure_format(shell, cfg.figure_format, cfg.quality) | |
|
342 | 346 |
@@ -14,13 +14,19 b' This module does not import anything from matplotlib.' | |||
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | from IPython.config.configurable import SingletonConfigurable |
|
17 | from IPython.utils.traitlets import Dict, Instance, CaselessStrEnum, Bool | |
|
17 | from IPython.utils.traitlets import Dict, Instance, CaselessStrEnum, Bool, Int | |
|
18 | 18 | from IPython.utils.warn import warn |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Configurable for inline backend options |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 | try: | |
|
25 | from PIL import Image | |
|
26 | has_pil = True | |
|
27 | except: | |
|
28 | has_pil = False | |
|
29 | ||
|
24 | 30 | # inherit from InlineBackendConfig for deprecation purposes |
|
25 | 31 | class InlineBackendConfig(SingletonConfigurable): |
|
26 | 32 | pass |
@@ -53,7 +59,20 b' class InlineBackend(InlineBackendConfig):' | |||
|
53 | 59 | inline backend.""" |
|
54 | 60 | ) |
|
55 | 61 | |
|
56 | figure_format = CaselessStrEnum(['svg', 'png', 'retina'], default_value='png', config=True, | |
|
62 | fmts = ['svg', 'png', 'retina'] | |
|
63 | ||
|
64 | if has_pil: | |
|
65 | # If we have PIL using jpeg as inline image format can save some bytes. | |
|
66 | fmts.append('jpg') | |
|
67 | ||
|
68 | # Matplotlib's JPEG printer supports a quality option that can be tweaked. | |
|
69 | # We expose it only if PIL is available so the user isn't confused. But it | |
|
70 | # isn't guarded by "has_pil" test because core/pylabtools.py expects this | |
|
71 | # field OR we need to propagate the has_pil test to that module too. | |
|
72 | quality = Int(default_value=90, config=has_pil, | |
|
73 | help="Quality of compression [0-100], currently for lossy JPEG only.") | |
|
74 | ||
|
75 | figure_format = CaselessStrEnum(fmts, default_value='png', config=True, | |
|
57 | 76 | help="The image format for figures with the inline backend.") |
|
58 | 77 | |
|
59 | 78 | def _figure_format_changed(self, name, old, new): |
General Comments 0
You need to be logged in to leave comments.
Login now