Revision: 8523
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8523&view=rev
Author:   mdboom
Date:     2010-07-06 20:25:51 +0000 (Tue, 06 Jul 2010)

Log Message:
-----------
Support optional JPEG and TIFF saving if PIL is installed.

Modified Paths:
--------------
    trunk/matplotlib/FILETYPES
    trunk/matplotlib/lib/matplotlib/backend_bases.py
    trunk/matplotlib/lib/matplotlib/backends/backend_agg.py

Modified: trunk/matplotlib/FILETYPES
===================================================================
--- trunk/matplotlib/FILETYPES  2010-07-06 15:49:23 UTC (rev 8522)
+++ trunk/matplotlib/FILETYPES  2010-07-06 20:25:51 UTC (rev 8523)
@@ -5,30 +5,33 @@
 
 Each cell specifies the backend that actually handles the file format.
 A cell with a '+' in it denotes the rasterizer and the file writing
-infrastructure as separate pieces.  
+infrastructure as separate pieces.
 
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 |        |bmp  |emf  |eps  |jpeg   |pcx  |pdf   |png   |ps   |raw  |svg  |svgz 
|tiff |xpm  |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|Agg     |     |emf  |ps   |       |     |pdf   |agg * |ps   |agg  |svg  |svg  
|     |     |
+|Agg     |     |emf  |ps   |agg +  |     |pdf   |agg * |ps   |agg  |svg  |svg  
|agg +|     |
+|        |     |     |     |pil    |     |      |      |     |     |     |     
|pil  |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 |Cairo   |     |emf  |ps   |       |     |cairo |cairo |cairo|agg  
|cairo|cairo|     |     |
 |[1]     |     |     |[2]  |       |     |      |*     |     |     |     |     
|     |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|CocoaAgg|     |emf  |ps   |       |     |pdf   |agg * |ps   |agg  |svg  |svg  
|     |     |
+|CocoaAgg|     |emf  |ps   |agg +  |     |pdf   |agg * |ps   |agg  |svg  |svg  
|agg +|     |
+|        |     |     |     |pil    |     |      |      |     |     |     |     
|pil  |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 |Emf     |     |emf *|     |       |     |      |      |     |     |     |     
|     |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|FltkAgg |     |emf  |ps   |       |     |pdf   |agg * |ps   |agg  |svg  |svg  
|     |     |
+|FltkAgg |     |emf  |ps   |agg +  |     |pdf   |agg * |ps   |agg  |svg  |svg  
|agg +|     |
+|        |     |     |     |pil    |     |      |      |     |     |     |     
|pil  |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 |Gd      |     |     |     |       |     |      |gd *  |     |     |     |     
|     |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|Gtk     |     |emf  |ps   |gdk +  |     |pdf   |gdk + |ps   |agg  |svg  |svg  
|     |     |
-|(gdk)   |     |     |     |pixbuf |     |      |pixbuf|     |     |     |     
|     |     |
+|Gtk     |     |emf  |ps   |gdk +  |     |pdf   |gdk + |ps   |agg  |svg  |svg  
|agg +|     |
+|(gdk)   |     |     |     |pixbuf |     |      |pixbuf|     |     |     |     
|pil  |     |
 |        |     |     |     |       |     |      |*     |     |     |     |     
|     |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|GtkAgg  |     |emf  |ps   |agg +  |     |pdf   |agg + |ps   |agg  |svg  |svg  
|     |     |
-|        |     |     |     |pixbuf |     |      |pixbuf|     |     |     |     
|     |     |
+|GtkAgg  |     |emf  |ps   |agg +  |     |pdf   |agg + |ps   |agg  |svg  |svg  
|agg +|     |
+|        |     |     |     |pixbuf |     |      |pixbuf|     |     |     |     
|pil  |     |
 |        |     |     |     |       |     |      |*     |     |     |     |     
|     |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 |GtkCairo|     |emf  |ps   |cairo +|     |cairo |cairo |cairo|agg  
|cairo|cairo|     |     |
@@ -41,18 +44,22 @@
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 |Ps      |     |     |ps   |       |     |      |      |ps * |     |     |     
|     |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|QtAgg   |     |emf  |ps   |       |     |pdf   |agg * |ps   |agg  |svg  |svg  
|     |     |
+|QtAgg   |     |emf  |ps   |agg +  |     |pdf   |agg * |ps   |agg  |svg  |svg  
|agg +|     |
+|        |     |     |     |pil    |     |      |      |     |     |     |     
|pil  |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|Qt4Agg  |     |emf  |ps   |       |     |pdf   |agg * |ps   |agg  |svg  |svg  
|     |     |
+|Qt4Agg  |     |emf  |ps   |agg +  |     |pdf   |agg * |ps   |agg  |svg  |svg  
|agg +|     |
+|        |     |     |     |pil    |     |      |      |     |     |     |     
|pil  |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 |Svg     |     |     |     |       |     |      |      |     |     |svg *|svg  
|     |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|TkAgg   |     |emf  |ps   |       |     |pdf   |agg * |ps   |agg  |svg  |svg  
|     |     |
+|TkAgg   |     |emf  |ps   |agg +  |     |pdf   |agg * |ps   |agg  |svg  |svg  
|agg +|     |
+|        |     |     |     |pil    |     |      |      |     |     |     |     
|pil  |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 |Wx      |wx + |emf  |ps   |wx + wx|wx + |pdf   |wx +  |ps   |agg  |svg  |svg  
|wx + |wx + |
 |        |wx   |     |     |       |wx   |      |wx *  |     |     |     |     
|wx   |wx   |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
-|WxAgg   |     |emf  |ps   |       |     |pdf   |agg * |ps   |agg  |svg  |svg  
|     |     |
+|WxAgg   |     |emf  |ps   |agg +  |     |pdf   |agg * |ps   |agg  |svg  |svg  
|agg +|     |
+|        |     |     |     |pil    |     |      |      |     |     |     |     
|pil  |     |
 
+--------+-----+-----+-----+-------+-----+------+------+-----+-----+-----+-----+-----+-----+
 
 * Default filetype for the backend

Modified: trunk/matplotlib/lib/matplotlib/backend_bases.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backend_bases.py    2010-07-06 15:49:23 UTC 
(rev 8522)
+++ trunk/matplotlib/lib/matplotlib/backend_bases.py    2010-07-06 20:25:51 UTC 
(rev 8523)
@@ -41,8 +41,12 @@
 import matplotlib.textpath as textpath
 from matplotlib.path import Path
 
+try:
+    import Image
+    _has_pil = True
+except ImportError:
+    _has_pil = False
 
-
 _backend_d = {}
 
 def register_backend(format, backend_class):
@@ -1721,6 +1725,40 @@
         svg = self.switch_backends(FigureCanvasSVG)
         return svg.print_svgz(*args, **kwargs)
 
+    if _has_pil:
+        filetypes['jpg'] = filetypes['jpeg'] = 'Joint Photographic Experts 
Group'
+        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.
+
+            *optimize*: If present, indicates that the encoder should
+                make an extra pass over the image in order to select
+                optimal encoder settings.
+
+            *progressive*: If present, indicates that this image
+                should be stored as a progressive JPEG file.
+            """
+            from backends.backend_agg import FigureCanvasAgg # lazy import
+            agg = self.switch_backends(FigureCanvasAgg)
+            buf, size = agg.print_to_buffer()
+            image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
+            return image.save(filename_or_obj, **kwargs)
+        print_jpeg = print_jpg
+
+        filetypes['tif'] = filetypes['tiff'] = 'Tagged Image File Format'
+        def print_tif(self, filename_or_obj, *args, **kwargs):
+            from backends.backend_agg import FigureCanvasAgg # lazy import
+            agg = self.switch_backends(FigureCanvasAgg)
+            buf, size = agg.print_to_buffer()
+            image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
+            return image.save(filename_or_obj)
+        print_tiff = print_tif
+
     def get_supported_filetypes(self):
         return self.filetypes
 

Modified: trunk/matplotlib/lib/matplotlib/backends/backend_agg.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backends/backend_agg.py     2010-07-06 
15:49:23 UTC (rev 8522)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_agg.py     2010-07-06 
20:25:51 UTC (rev 8523)
@@ -445,3 +445,13 @@
                        renderer.width, renderer.height,
                        filename_or_obj, self.figure.dpi)
         renderer.dpi = original_dpi
+
+    def print_to_buffer(self):
+        FigureCanvasAgg.draw(self)
+        renderer = self.get_renderer()
+        original_dpi = renderer.dpi
+        renderer.dpi = self.figure.dpi
+        result = (renderer._renderer.buffer_rgba(0, 0),
+                  (int(renderer.width), int(renderer.height)))
+        renderer.dpi = original_dpi
+        return result


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to