]> git.parisson.com Git - timeside.git/commitdiff
Grapher: attempt to fix#43
authorThomas Fillon <thomas@parisson.com>
Thu, 6 Mar 2014 17:45:26 +0000 (18:45 +0100)
committerThomas Fillon <thomas@parisson.com>
Thu, 6 Mar 2014 17:45:26 +0000 (18:45 +0100)
timeside/analyzer/core.py
timeside/grapher/render_analyzers.py

index f0e649f3cff1bbdbc8dd67531f963d5f4450e14d..095a1ef4b9b5aa770643c1f53ba40ce869476e16 100644 (file)
@@ -31,10 +31,15 @@ import h5py
 import h5tools
 
 import os
+import matplotlib
+matplotlib.use('Agg')
 
-if 'DISPLAY' not in os.environ:
-    import matplotlib
-    matplotlib.use('Agg')
+from matplotlib.figure import Figure
+from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
+
+#if 'DISPLAY' not in os.environ:
+#    import matplotlib
+#    matplotlib.use('Agg')
 
 import matplotlib.pyplot as plt
 
@@ -624,25 +629,29 @@ class AnalyzerResult(MetadataObject):
     def _render_plot(self, ax):
         return NotImplemented
 
-    def render(self, size=(1024, 256), dpi=80):
-
+    def _render_PIL(self, size=(1024, 256), dpi=80):
+        from ..grapher.core import Image
         image_width, image_height = size
 
         xSize = image_width / dpi
         ySize = image_height / dpi
 
-        fig = plt.figure(figsize=(xSize, ySize), dpi=dpi)
+        fig = Figure(figsize=(xSize, ySize), dpi=dpi)
+
+        ax = fig.add_axes([0, 0, 1, 1], frame_on=False)
 
-        ax = plt.Axes(fig, [0, 0, 1, 1])
-        ax.set_frame_on(False)
         self._render_plot(ax)
 
-#        ax.axis('off')
-       # ax.axis('tight')
         ax.autoscale(axis='x', tight=True)
-        fig.add_axes(ax)
 
-        return fig
+        # Export to PIL image
+        from StringIO import StringIO
+        imgdata = StringIO()
+        canvas = FigureCanvas(fig)
+        canvas.print_png(imgdata, dpi=dpi)
+        imgdata.seek(0)  # rewind the data
+
+        return Image.open(imgdata)
 
     @property
     def data_mode(self):
index 063a0a4b0215109643168675de3fdd7aae25273c..b2fa143e74ee80d35d6323dc7c3d9621540a7033 100644 (file)
@@ -40,8 +40,7 @@ class DisplayAnalyzer(Grapher):
     def __init__(self, width=1024, height=256, bg_color=(0, 0, 0),
                  color_scheme='default'):
         super(DisplayAnalyzer, self).__init__(width, height, bg_color,
-                                               color_scheme)
-        #self.dpi = 72
+                                              color_scheme)
 
         self._result_id = None
         self._id = NotImplemented
@@ -55,22 +54,14 @@ class DisplayAnalyzer(Grapher):
     def post_process(self):
         parent_result = self.process_pipe.results[self._result_id]
 
-        fig = parent_result.render((self.image_width,
-                                    self.image_height), self.dpi)
-
-        # Export to PIL image
-        import StringIO
-        imgdata = StringIO.StringIO()
-        fig.savefig(imgdata, format='png', dpi=self.dpi)
-        imgdata.seek(0)  # rewind the data
-        self.image = Image.open(imgdata)
+        self.image = parent_result._render_PIL((self.image_width,
+                                                self.image_height), self.dpi)
 
     @classmethod
     def create(cls, analyzer, result_id, grapher_id, grapher_name):
 
         class NewGrapher(cls):
 
-
             _id = grapher_id
 
             implements(IGrapher)