From a6c42aba850132cd2d2688b58d6940f8ea5fd6f6 Mon Sep 17 00:00:00 2001 From: Thomas Fillon Date: Thu, 6 Mar 2014 18:45:26 +0100 Subject: [PATCH] Grapher: attempt to fix #43 --- timeside/analyzer/core.py | 33 ++++++++++++++++++---------- timeside/grapher/render_analyzers.py | 15 +++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/timeside/analyzer/core.py b/timeside/analyzer/core.py index f0e649f..095a1ef 100644 --- a/timeside/analyzer/core.py +++ b/timeside/analyzer/core.py @@ -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): diff --git a/timeside/grapher/render_analyzers.py b/timeside/grapher/render_analyzers.py index 063a0a4..b2fa143 100644 --- a/timeside/grapher/render_analyzers.py +++ b/timeside/grapher/render_analyzers.py @@ -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) -- 2.39.5