[Python-modules-commits] [python-mplexporter] 34/135: add draw_image

Wolfgang Borgert debacle at moszumanska.debian.org
Tue Sep 23 21:19:01 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to branch master
in repository python-mplexporter.

commit b47a74540900b0ac985d63aa7d4e9a6b1a3b0ec1
Author: Jake Vanderplas <vanderplas at astro.washington.edu>
Date:   Sun Feb 23 20:36:26 2014 -0800

    add draw_image
---
 mplexporter/exporter.py       | 27 +++++++++++++++++++++++++++
 mplexporter/renderers/base.py | 15 +++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/mplexporter/exporter.py b/mplexporter/exporter.py
index 153c205..a2b957b 100644
--- a/mplexporter/exporter.py
+++ b/mplexporter/exporter.py
@@ -5,6 +5,7 @@ This submodule contains tools for crawling a matplotlib figure and exporting
 relevant pieces to a renderer.
 """
 import io
+import base64
 from . import utils
 
 
@@ -79,6 +80,23 @@ class Exporter(object):
             else:
                 return code
 
+    @staticmethod
+    def image_base64_data(image):
+        ax = image.axes
+        binary_buffer = io.BytesIO()
+
+        # image is saved in axes coordinates: we need to temporarily
+        # set the correct limits to get the correct image, then undo it.
+        xlim = ax.get_xlim()
+        ylim = ax.get_ylim()
+        ax.set_xlim(image.get_extent()[:2])
+        ax.set_ylim(image.get_extent()[2:])
+        image.write_png(binary_buffer)
+        ax.set_xlim(xlim)
+        ax.set_ylim(ylim)
+        binary_buffer.seek(0)
+        return base64.b64encode(binary_buffer.read()).decode('utf-8')
+
     def _crawl_fig(self, fig):
         properties = {'figwidth': fig.get_figwidth(),
                       'figheight': fig.get_figheight(),
@@ -104,6 +122,7 @@ class Exporter(object):
             self._extract_patches(ax)
             self._extract_texts(ax)
             self._extract_collections(ax)
+            self._extract_images(ax)
 
     def _extract_lines(self, ax):
         for line in ax.lines:
@@ -181,3 +200,11 @@ class Exporter(object):
                                                offset_coordinates,
                                                offset_order,
                                                styles)
+
+    def _extract_images(self, ax):
+        for image in ax.images:
+            imdata = self.image_base64_data(image)
+            self.renderer.draw_image(imdata=self.image_base64_data(image),
+                                     extent=image.get_extent(),
+                                     coordinates="data",
+                                     style={"alpha": image.get_alpha()})
diff --git a/mplexporter/renderers/base.py b/mplexporter/renderers/base.py
index c37e2db..7ee0ea6 100644
--- a/mplexporter/renderers/base.py
+++ b/mplexporter/renderers/base.py
@@ -254,4 +254,19 @@ class Renderer(object):
         raise NotImplementedError()
 
     def draw_image(self, imdata, extent, coordinates, style):
+        """
+        Draw an image.
+
+        Parameters
+        ----------
+        imdata : string
+            base64 encoded png representation of the image
+        extent : list
+            the axes extent of the image: [xmin, xmax, ymin, ymax]
+        coordinates: string
+            A string code, which should be either 'data' for data coordinates,
+            or 'figure' for figure (pixel) coordinates.
+        style : dictionary
+            a dictionary specifying the appearance of the image
+        """
         raise NotImplementedError()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-mplexporter.git



More information about the Python-modules-commits mailing list