示例#1
0
 def stop_rasterizing(self):
     """
     Exit "raster" mode.  All of the drawing that was done since
     the last start_rasterizing command will be copied to the
     vector backend by calling draw_image.
     If stop_rasterizing is called multiple times before
     start_rasterizing is called, this method has no effect.
     """
     self._rasterizing -= 1
     if self._rasterizing == 0:
         self._set_current_renderer(self._vector_renderer)
         width, height = self._width * self.dpi, self._height * self.dpi
         buffer, bounds = self._raster_renderer.tostring_rgba_minimized()
         l, b, w, h = bounds
         if w > 0 and h > 0:
             image = frombuffer(buffer, w, h, True)
             image.is_grayscale = False
             image.flipud_out()
             self._renderer.draw_image(
                 float(l)/self.dpi*72.,
                 (float(height) - b - h)/self.dpi*72.,
                 image, None)
         self._raster_renderer = None
         self._rasterizing = False
     self.figure.set_dpi(72)
     if self._bbox_inches_restore:  # when tight bbox is used
         r = process_figure_for_rasterizing(self.figure,
                                            self._bbox_inches_restore,
                                            mode="pdf")
         self._bbox_inches_restore = r
示例#2
0
    def stop_rasterizing(self):
        """
        Exit "raster" mode.  All of the drawing that was done since
        the last start_rasterizing command will be copied to the
        vector backend by calling draw_image.

        If stop_rasterizing is called multiple times before
        start_rasterizing is called, this method has no effect.
        """
        self._rasterizing -= 1
        if self._rasterizing == 0:
            self._set_current_renderer(self._vector_renderer)

            width, height = self._width * self.dpi, self._height * self.dpi
            buffer, bounds = self._raster_renderer.tostring_rgba_minimized()
            l, b, w, h = bounds
            if w > 0 and h > 0:
                image = frombuffer(buffer, w, h, True)
                image.is_grayscale = False
                image.flipud_out()
                self._renderer.draw_image(
                    int(float(l) / self.dpi * 72.),
                    int((float(height) - b - h) / self.dpi * 72.), image, None)
            self._raster_renderer = None
            self._rasterizing = False

        # restore the figure dpi.
        self.figure.set_dpi(72)

        if self._bbox_inches_restore:  # when tight bbox is used
            r = process_figure_for_rasterizing(self.figure,
                                               self._bbox_inches_restore,
                                               mode="pdf")
            self._bbox_inches_restore = r
示例#3
0
    def stop_rasterizing(self):
        """
        Exit "raster" mode.  All of the drawing that was done since
        the last start_rasterizing command will be copied to the
        vector backend by calling draw_image.

        If stop_rasterizing is called multiple times before
        start_rasterizing is called, this method has no effect.
        """
        self._rasterizing -= 1
        if self._rasterizing == 0:
            self._set_current_renderer(self._vector_renderer)

            width, height = self._width * self.dpi, self._height * self.dpi
            buffer, bounds = self._raster_renderer.tostring_rgba_minimized()
            l, b, w, h = bounds
            if w > 0 and h > 0:
                image = frombuffer(buffer, w, h, True)
                image.is_grayscale = False
                image.flipud_out()
                gc = self._renderer.new_gc()
                # TODO: If the mixedmode resolution differs from the figure's
                #       dpi, the image must be scaled (dpi->_figdpi). Not all
                #       backends support this.
                self._renderer.draw_image(
                    gc,
                    float(l) / self.dpi * self._figdpi,
                    (float(height)-b-h) / self.dpi * self._figdpi,
                    image)
            self._raster_renderer = None
            self._rasterizing = False

            # restore the figure dpi.
            self.figure.set_dpi(self._figdpi)

        if self._bbox_inches_restore:  # when tight bbox is used
            r = process_figure_for_rasterizing(self.figure,
                                               self._bbox_inches_restore,
                                               mode="pdf")
            self._bbox_inches_restore = r
示例#4
0
    def stop_rasterizing(self):
        """
        Exit "raster" mode.  All of the drawing that was done since
        the last start_rasterizing command will be copied to the
        vector backend by calling draw_image.

        If stop_rasterizing is called multiple times before
        start_rasterizing is called, this method has no effect.
        """
        self._rasterizing -= 1
        if self._rasterizing == 0:
            self._set_current_renderer(self._vector_renderer)

            width, height = self._width * self.dpi, self._height * self.dpi
            buffer, bounds = self._raster_renderer.tostring_rgba_minimized()
            l, b, w, h = bounds
            if w > 0 and h > 0:
                image = frombuffer(buffer, w, h, True)
                image.is_grayscale = False
                image.flipud_out()
                gc = self._renderer.new_gc()
                # TODO: If the mixedmode resolution differs from the figure's
                #       dpi, the image must be scaled (dpi->_figdpi). Not all
                #       backends support this.
                self._renderer.draw_image(
                    gc,
                    float(l) / self.dpi * self._figdpi,
                    (float(height)-b-h) / self.dpi * self._figdpi,
                    image)
            self._raster_renderer = None
            self._rasterizing = False

            # restore the figure dpi.
            self.figure.set_dpi(self._figdpi)

        if self._bbox_inches_restore:  # when tight bbox is used
            r = process_figure_for_rasterizing(self.figure,
                                               self._bbox_inches_restore,
                                               self._figdpi)
            self._bbox_inches_restore = r
示例#5
0
    def stop_rasterizing(self):
        """
        Exit "raster" mode.  All of the drawing that was done since
        the last start_rasterizing command will be copied to the
        vector backend by calling draw_image.

        If stop_rasterizing is called multiple times before
        start_rasterizing is called, this method has no effect.
        """
        if self._rasterizing:
            self._set_current_renderer(self._vector_renderer)

            width, height = self._width * self._dpi, self._height * self._dpi
            buffer, bounds = self._raster_renderer.tostring_rgba_minimized()
            l, b, w, h = bounds
            if w > 0 and h > 0:
                image = frombuffer(buffer, w, h, True)
                image.is_grayscale = False

                self._renderer.draw_image(l, height - b - h, image, None)
            self._raster_renderer = None
            self._rasterizing = False
示例#6
0
    def stop_rasterizing(self):
        """
        Exit "raster" mode.  All of the drawing that was done since
        the last start_rasterizing command will be copied to the
        vector backend by calling draw_image.

        If stop_rasterizing is called multiple times before
        start_rasterizing is called, this method has no effect.
        """
        if self._rasterizing:
            self._set_current_renderer(self._vector_renderer)

            width, height = self._width * self._dpi, self._height * self._dpi
            buffer, bounds = self._raster_renderer.tostring_rgba_minimized()
            l, b, w, h = bounds
            if w > 0 and h > 0:
                image = frombuffer(buffer, w, h, True)
                image.is_grayscale = False

                self._renderer.draw_image(l, height - b - h, image, None)
            self._raster_renderer = None
            self._rasterizing = False
示例#7
0
    def device_draw_image(self, img, rect):
        """
        draw_image(img_gc, rect=(x,y,w,h))

        Draws another gc into this one.  If 'rect' is not provided, then
        the image gc is drawn into this one, rooted at (0,0) and at full
        pixel size.  If 'rect' is provided, then the image is resized
        into the (w,h) given and drawn into this GC at point (x,y).

        img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's
        Agg backend (kiva.agg.GraphicsContextArray).

        Requires the Python Imaging Library (PIL).
        """
        from PIL import Image as PilImage
        from matplotlib import _image

        # We turn img into a PIL object, since that is what ReportLab
        # requires.  To do this, we first determine if the input image
        # GC needs to be converted to RGBA/RGB.  If so, we see if we can
        # do it nicely (using convert_pixel_format), and if not, we do
        # it brute-force using Agg.
        if type(img) == type(array([])):
            # Numeric array
            converted_img = agg.GraphicsContextArray(img, pix_format='rgba32')
            format = 'RGBA'
        elif isinstance(img, agg.GraphicsContextArray):
            if img.format().startswith('RGBA'):
                format = 'RGBA'
            elif img.format().startswith('RGB'):
                format = 'RGB'
            else:
                converted_img = img.convert_pixel_format('rgba32', inplace=0)
                format = 'RGBA'
            # Should probably take this into account
            # interp = img.get_image_interpolation()
        else:
            warnings.warn("Cannot render image of type %r into SVG context."
                          % type(img))
            return

        if rect == None:
            rect = (0, 0, img.width(), img.height())

        width, height = img.width(), img.height()

        # converted_img now holds an Agg graphics context with the image
        pil_img = PilImage.fromstring(format,
                                      (converted_img.width(),
                                       converted_img.height()),
                                      converted_img.bmp_array.tostring())

        left, top, width, height = rect
        if width != img.width() or height != img.height():
            # This is not strictly required.
            pil_img = pil_img.resize((int(width), int(height)), PilImage.NEAREST)
        pil_img = pil_img.transpose(PilImage.FLIP_TOP_BOTTOM)
        # Fix for the SVG backend, which seems to flip x when a transform is provided.
        if self._backend.flipy():
            pil_img = pil_img.transpose(PilImage.FLIP_LEFT_RIGHT)

        mpl_img = _image.frombuffer(pil_img.tostring(), width, height, True)
        mpl_img.is_grayscale = False

        gc = self._backend.new_gc()
        if self.state.clipping_path:
            gc.set_clip_path(self._get_transformed_clip_path())
        transform = Affine2D.from_values(*affine.affine_params(self.get_ctm()))
        self._backend.draw_image(gc, left, top, mpl_img,
                                 dx=width, dy=height, transform=transform)
        gc.restore()
示例#8
0
文件: mpl.py 项目: 0thm4n/pdviper
    def device_draw_image(self, img, rect):
        """
        draw_image(img_gc, rect=(x,y,w,h))

        Draws another gc into this one.  If 'rect' is not provided, then
        the image gc is drawn into this one, rooted at (0,0) and at full
        pixel size.  If 'rect' is provided, then the image is resized
        into the (w,h) given and drawn into this GC at point (x,y).

        img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's
        Agg backend (kiva.agg.GraphicsContextArray).

        Requires the Python Imaging Library (PIL).
        """
        from PIL import Image as PilImage
        from matplotlib import _image

        # We turn img into a PIL object, since that is what ReportLab
        # requires.  To do this, we first determine if the input image
        # GC needs to be converted to RGBA/RGB.  If so, we see if we can
        # do it nicely (using convert_pixel_format), and if not, we do
        # it brute-force using Agg.
        if type(img) == type(array([])):
            # Numeric array
            converted_img = agg.GraphicsContextArray(img, pix_format='rgba32')
            format = 'RGBA'
        elif isinstance(img, agg.GraphicsContextArray):
            if img.format().startswith('RGBA'):
                format = 'RGBA'
            elif img.format().startswith('RGB'):
                format = 'RGB'
            else:
                converted_img = img.convert_pixel_format('rgba32', inplace=0)
                format = 'RGBA'
            # Should probably take this into account
            # interp = img.get_image_interpolation()
        else:
            warnings.warn("Cannot render image of type %r into SVG context." %
                          type(img))
            return

        if rect == None:
            rect = (0, 0, img.width(), img.height())

        width, height = img.width(), img.height()

        # converted_img now holds an Agg graphics context with the image
        pil_img = PilImage.fromstring(
            format, (converted_img.width(), converted_img.height()),
            converted_img.bmp_array.tostring())

        left, top, width, height = rect
        if width != img.width() or height != img.height():
            # This is not strictly required.
            pil_img = pil_img.resize((int(width), int(height)),
                                     PilImage.NEAREST)
        pil_img = pil_img.transpose(PilImage.FLIP_TOP_BOTTOM)
        # Fix for the SVG backend, which seems to flip x when a transform is provided.
        if self._backend.flipy():
            pil_img = pil_img.transpose(PilImage.FLIP_LEFT_RIGHT)

        mpl_img = _image.frombuffer(pil_img.tostring(), width, height, True)
        mpl_img.is_grayscale = False

        gc = self._backend.new_gc()
        if self.state.clipping_path:
            gc.set_clip_path(self._get_transformed_clip_path())
        transform = Affine2D.from_values(*affine.affine_params(self.get_ctm()))
        self._backend.draw_image(gc,
                                 left,
                                 top,
                                 mpl_img,
                                 dx=width,
                                 dy=height,
                                 transform=transform)
        gc.restore()