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

Skip to content

Commit 852b41c

Browse files
committed
Support optional JPEG and TIFF saving if PIL is installed.
svn path=/trunk/matplotlib/; revision=8523
1 parent 379d0a9 commit 852b41c

3 files changed

Lines changed: 68 additions & 13 deletions

File tree

FILETYPES

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,33 @@ correctly. It may be edited with emacs' table mode.
55

66
Each cell specifies the backend that actually handles the file format.
77
A cell with a '+' in it denotes the rasterizer and the file writing
8-
infrastructure as separate pieces.
8+
infrastructure as separate pieces.
99

1010
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
1111
| |bmp |emf |eps |jpeg |pcx |pdf |png |ps |raw |svg |svgz |tiff |xpm |
1212
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
13-
|Agg | |emf |ps | | |pdf |agg * |ps |agg |svg |svg | | |
13+
|Agg | |emf |ps |agg + | |pdf |agg * |ps |agg |svg |svg |agg +| |
14+
| | | | |pil | | | | | | | |pil | |
1415
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
1516
|Cairo | |emf |ps | | |cairo |cairo |cairo|agg |cairo|cairo| | |
1617
|[1] | | |[2] | | | |* | | | | | | |
1718
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
18-
|CocoaAgg| |emf |ps | | |pdf |agg * |ps |agg |svg |svg | | |
19+
|CocoaAgg| |emf |ps |agg + | |pdf |agg * |ps |agg |svg |svg |agg +| |
20+
| | | | |pil | | | | | | | |pil | |
1921
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
2022
|Emf | |emf *| | | | | | | | | | | |
2123
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
22-
|FltkAgg | |emf |ps | | |pdf |agg * |ps |agg |svg |svg | | |
24+
|FltkAgg | |emf |ps |agg + | |pdf |agg * |ps |agg |svg |svg |agg +| |
25+
| | | | |pil | | | | | | | |pil | |
2326
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
2427
|Gd | | | | | | |gd * | | | | | | |
2528
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
26-
|Gtk | |emf |ps |gdk + | |pdf |gdk + |ps |agg |svg |svg | | |
27-
|(gdk) | | | |pixbuf | | |pixbuf| | | | | | |
29+
|Gtk | |emf |ps |gdk + | |pdf |gdk + |ps |agg |svg |svg |agg +| |
30+
|(gdk) | | | |pixbuf | | |pixbuf| | | | |pil | |
2831
| | | | | | | |* | | | | | | |
2932
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
30-
|GtkAgg | |emf |ps |agg + | |pdf |agg + |ps |agg |svg |svg | | |
31-
| | | | |pixbuf | | |pixbuf| | | | | | |
33+
|GtkAgg | |emf |ps |agg + | |pdf |agg + |ps |agg |svg |svg |agg +| |
34+
| | | | |pixbuf | | |pixbuf| | | | |pil | |
3235
| | | | | | | |* | | | | | | |
3336
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
3437
|GtkCairo| |emf |ps |cairo +| |cairo |cairo |cairo|agg |cairo|cairo| | |
@@ -41,18 +44,22 @@ infrastructure as separate pieces.
4144
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
4245
|Ps | | |ps | | | | |ps * | | | | | |
4346
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
44-
|QtAgg | |emf |ps | | |pdf |agg * |ps |agg |svg |svg | | |
47+
|QtAgg | |emf |ps |agg + | |pdf |agg * |ps |agg |svg |svg |agg +| |
48+
| | | | |pil | | | | | | | |pil | |
4549
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
46-
|Qt4Agg | |emf |ps | | |pdf |agg * |ps |agg |svg |svg | | |
50+
|Qt4Agg | |emf |ps |agg + | |pdf |agg * |ps |agg |svg |svg |agg +| |
51+
| | | | |pil | | | | | | | |pil | |
4752
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
4853
|Svg | | | | | | | | | |svg *|svg | | |
4954
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
50-
|TkAgg | |emf |ps | | |pdf |agg * |ps |agg |svg |svg | | |
55+
|TkAgg | |emf |ps |agg + | |pdf |agg * |ps |agg |svg |svg |agg +| |
56+
| | | | |pil | | | | | | | |pil | |
5157
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
5258
|Wx |wx + |emf |ps |wx + wx|wx + |pdf |wx + |ps |agg |svg |svg |wx + |wx + |
5359
| |wx | | | |wx | |wx * | | | | |wx |wx |
5460
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
55-
|WxAgg | |emf |ps | | |pdf |agg * |ps |agg |svg |svg | | |
61+
|WxAgg | |emf |ps |agg + | |pdf |agg * |ps |agg |svg |svg |agg +| |
62+
| | | | |pil | | | | | | | |pil | |
5663
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
5764

5865
* Default filetype for the backend

lib/matplotlib/backend_bases.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141
import matplotlib.textpath as textpath
4242
from matplotlib.path import Path
4343

44-
44+
try:
45+
import Image
46+
_has_pil = True
47+
except ImportError:
48+
_has_pil = False
4549

4650
_backend_d = {}
4751

@@ -1721,6 +1725,40 @@ def print_svgz(self, *args, **kwargs):
17211725
svg = self.switch_backends(FigureCanvasSVG)
17221726
return svg.print_svgz(*args, **kwargs)
17231727

1728+
if _has_pil:
1729+
filetypes['jpg'] = filetypes['jpeg'] = 'Joint Photographic Experts Group'
1730+
def print_jpg(self, filename_or_obj, *args, **kwargs):
1731+
"""
1732+
Supported kwargs:
1733+
1734+
*quality*: The image quality, on a scale from 1 (worst) to
1735+
95 (best). The default is 75. Values above 95 should
1736+
be avoided; 100 completely disables the JPEG
1737+
quantization stage.
1738+
1739+
*optimize*: If present, indicates that the encoder should
1740+
make an extra pass over the image in order to select
1741+
optimal encoder settings.
1742+
1743+
*progressive*: If present, indicates that this image
1744+
should be stored as a progressive JPEG file.
1745+
"""
1746+
from backends.backend_agg import FigureCanvasAgg # lazy import
1747+
agg = self.switch_backends(FigureCanvasAgg)
1748+
buf, size = agg.print_to_buffer()
1749+
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
1750+
return image.save(filename_or_obj, **kwargs)
1751+
print_jpeg = print_jpg
1752+
1753+
filetypes['tif'] = filetypes['tiff'] = 'Tagged Image File Format'
1754+
def print_tif(self, filename_or_obj, *args, **kwargs):
1755+
from backends.backend_agg import FigureCanvasAgg # lazy import
1756+
agg = self.switch_backends(FigureCanvasAgg)
1757+
buf, size = agg.print_to_buffer()
1758+
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
1759+
return image.save(filename_or_obj)
1760+
print_tiff = print_tif
1761+
17241762
def get_supported_filetypes(self):
17251763
return self.filetypes
17261764

lib/matplotlib/backends/backend_agg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,13 @@ def print_png(self, filename_or_obj, *args, **kwargs):
445445
renderer.width, renderer.height,
446446
filename_or_obj, self.figure.dpi)
447447
renderer.dpi = original_dpi
448+
449+
def print_to_buffer(self):
450+
FigureCanvasAgg.draw(self)
451+
renderer = self.get_renderer()
452+
original_dpi = renderer.dpi
453+
renderer.dpi = self.figure.dpi
454+
result = (renderer._renderer.buffer_rgba(0, 0),
455+
(int(renderer.width), int(renderer.height)))
456+
renderer.dpi = original_dpi
457+
return result

0 commit comments

Comments
 (0)