Example #1
0
 def stop_filter(self, post_processing):
     """
     Save the plot in the current canvas as a image and apply
     the *post_processing* function.
        def post_processing(image, dpi):
          ...
          return new_image, offset_x, offset_y
     The saved renderer is restored and the returned image from
     post_processing is plotted (using draw_image) on it.
     """
     from matplotlib._image import fromarray
     width, height = int(self.width), int(self.height)
     buffer, bounds = self._renderer.tostring_rgba_minimized()
     l, b, w, h = bounds
     self._renderer = self._filter_renderers.pop()
     self._update_methods()
     if w > 0 and h > 0:
         img = npy.fromstring(buffer, npy.uint8)
         img, ox, oy = post_processing(img.reshape((h, w, 4)) / 255.,
                                       self.dpi)
         image = fromarray(img, 1)
         image.flipud_out()
         gc = self.new_gc()
         self._renderer.draw_image(gc,
                                   l+ox, height - b - h +oy,
                                   image)
Example #2
0
    def make_image(self, magnification=1.0):
        if self._A is None:
            raise RuntimeError('You must first set the image array')

        x = self.to_rgba(self._A, self._alpha)
        self.magnification = magnification
        # if magnification is not one, we need to resize
        ismag = magnification!=1
        #if ismag: raise RuntimeError
        if ismag:
            isoutput = 0
        else:
            isoutput = 1
        im = _image.fromarray(x, isoutput)
        fc = self.figure.get_facecolor()
        im.set_bg( *mcolors.colorConverter.to_rgba(fc, 0) )
        im.is_grayscale = (self.cmap.name == "gray" and
                           len(self._A.shape) == 2)

        if ismag:
            numrows, numcols = self.get_size()
            numrows *= magnification
            numcols *= magnification
            im.set_interpolation(_image.NEAREST)
            im.resize(numcols, numrows)
        if self.origin=='upper':
            im.flipud_out()

        return im
Example #3
0
    def make_image(self, magnification=1.0):
        if self._A is None:
            raise RuntimeError('You must first set the image array or the image attribute')

        if self._imcache is None:
            if self._A.dtype == npy.uint8 and len(self._A.shape) == 3:
                im = _image.frombyte(self._A, 0)
                im.is_grayscale = False
            else:
                x = self.to_rgba(self._A, self._alpha)
                im = _image.fromarray(x, 0)
                if len(self._A.shape) == 2:
                    im.is_grayscale = self.cmap.is_gray()
                else:
                    im.is_grayscale = False
            self._imcache = im

            if self.origin=='upper':
                im.flipud_in()
        else:
            im = self._imcache

        fc = self.axes.get_frame().get_facecolor()
        bg = mcolors.colorConverter.to_rgba(fc, 0)
        im.set_bg( *bg)

        # image input dimensions
        im.reset_matrix()
        numrows, numcols = im.get_size()

        im.set_interpolation(self._interpd[self._interpolation])

        xmin, xmax, ymin, ymax = self.get_extent()
        dxintv = xmax-xmin
        dyintv = ymax-ymin

        # the viewport scale factor
        sx = dxintv/self.axes.viewLim.width
        sy = dyintv/self.axes.viewLim.height

        # the viewport translation
        tx = (xmin-self.axes.viewLim.x0)/dxintv * numcols
        ty = (ymin-self.axes.viewLim.y0)/dyintv * numrows

        l, b, widthDisplay, heightDisplay = self.axes.bbox.bounds
        widthDisplay *= magnification
        heightDisplay *= magnification

        im.apply_translation(tx, ty)
        im.apply_scaling(sx, sy)

        # resize viewport to display
        rx = widthDisplay / numcols
        ry = heightDisplay  / numrows
        im.apply_scaling(rx, ry)

        im.resize(int(widthDisplay+0.5), int(heightDisplay+0.5),
                  norm=self._filternorm, radius=self._filterrad)

        return im
Example #4
0
    def make_image(self, magnification=1.0):
        if self._A is None:
            raise RuntimeError('You must first set the image array')

        x = self.to_rgba(self._A, self._alpha)
        self.magnification = magnification
        # if magnification is not one, we need to resize
        ismag = magnification != 1
        #if ismag: raise RuntimeError
        if ismag:
            isoutput = 0
        else:
            isoutput = 1
        im = _image.fromarray(x, isoutput)
        fc = self.figure.get_facecolor()
        im.set_bg(*mcolors.colorConverter.to_rgba(fc, 0))
        im.is_grayscale = (self.cmap.name == "gray"
                           and len(self._A.shape) == 2)

        if ismag:
            numrows, numcols = self.get_size()
            numrows *= magnification
            numcols *= magnification
            im.set_interpolation(_image.NEAREST)
            im.resize(numcols, numrows)
        if self.origin == 'upper':
            im.flipud_out()

        return im
Example #5
0
    def make_image(self, renderer, magnification=1.0):
        if self._A is None:
            raise RuntimeError(
                'You must first set the image array or the image attribute')

        if self._imcache is None:
            if self._A.dtype == np.uint8 and len(self._A.shape) == 3:
                im = _image.frombyte(self._A, 0)
                im.is_grayscale = False
            else:
                if self._rgbacache is None:
                    x = self.to_rgba(self._A, self._alpha)
                    self._rgbacache = x
                else:
                    x = self._rgbacache
                im = _image.fromarray(x, 0)
                if len(self._A.shape) == 2:
                    im.is_grayscale = self.cmap.is_gray()
                else:
                    im.is_grayscale = False
            self._imcache = im

            if self.origin == 'upper':
                im.flipud_in()
        else:
            im = self._imcache

        # image input dimensions
        im.reset_matrix()

        im.set_interpolation(self._interpd[self._interpolation])

        im.set_resample(self._resample)

        l, b, r, t = self.get_window_extent(renderer).extents  #bbox.extents
        widthDisplay = (round(r) + 0.5) - (round(l) - 0.5)
        heightDisplay = (round(t) + 0.5) - (round(b) - 0.5)
        widthDisplay *= magnification
        heightDisplay *= magnification

        numrows, numcols = self._A.shape[:2]

        # resize viewport to display
        rx = widthDisplay / numcols
        ry = heightDisplay / numrows
        #im.apply_scaling(rx*sx, ry*sy)
        im.apply_scaling(rx, ry)
        #im.resize(int(widthDisplay+0.5), int(heightDisplay+0.5),
        #          norm=self._filternorm, radius=self._filterrad)
        im.resize(int(widthDisplay),
                  int(heightDisplay),
                  norm=self._filternorm,
                  radius=self._filterrad)
        return im
Example #6
0
    def make_image(self, renderer, magnification=1.0):
        if self._A is None:
            raise RuntimeError('You must first set the image array or the image attribute')

        if self._imcache is None:
            if self._A.dtype == np.uint8 and len(self._A.shape) == 3:
                im = _image.frombyte(self._A, 0)
                im.is_grayscale = False
            else:
                if self._rgbacache is None:
                    x = self.to_rgba(self._A, self._alpha)
                    self._rgbacache = x
                else:
                    x = self._rgbacache
                im = _image.fromarray(x, 0)
                if len(self._A.shape) == 2:
                    im.is_grayscale = self.cmap.is_gray()
                else:
                    im.is_grayscale = False
            self._imcache = im

            if self.origin=='upper':
                im.flipud_in()
        else:
            im = self._imcache

        # image input dimensions
        im.reset_matrix()

        im.set_interpolation(self._interpd[self._interpolation])

        im.set_resample(self._resample)

        l, b, r, t = self.get_window_extent(renderer).extents #bbox.extents
        widthDisplay = (round(r) + 0.5) - (round(l) - 0.5)
        heightDisplay = (round(t) + 0.5) - (round(b) - 0.5)
        widthDisplay *= magnification
        heightDisplay *= magnification

        numrows, numcols = self._A.shape[:2]

        # resize viewport to display
        rx = widthDisplay / numcols
        ry = heightDisplay  / numrows
        #im.apply_scaling(rx*sx, ry*sy)
        im.apply_scaling(rx, ry)
        #im.resize(int(widthDisplay+0.5), int(heightDisplay+0.5),
        #          norm=self._filternorm, radius=self._filterrad)
        im.resize(int(widthDisplay), int(heightDisplay),
                  norm=self._filternorm, radius=self._filterrad)
        return im
Example #7
0
    def stop_filter(self, post_processing):
        return RendererBase.stop_filter(self, post_processing)
        """
        Save the plot in the current canvas as a image and apply
        the *post_processing* function.

           def post_processing(image, dpi):
             # ny, nx, depth = image.shape
             # image (numpy array) has RGBA channels and has a depth of 4.
             ...
             # create a new_image (numpy array of 4 channels, size can be
             # different). The resulting image may have offsets from
             # lower-left corner of the original image
             return new_image, offset_x, offset_y

        The saved renderer is restored and the returned image from
        post_processing is plotted (using draw_image) on it.
        """

        # WARNING.
        # For agg_filter to work, the rendere's method need
        # to overridden in the class. See draw_markers, and draw_path_collections

        from matplotlib._image import fromarray

        width, height = int(self.width), int(self.height)

        buffer, bounds = self._renderer.tostring_rgba_minimized()

        l, b, w, h = bounds


        self._renderer = self._filter_renderers.pop()
        self._update_methods()

        if w > 0 and h > 0:
            img = np.fromstring(buffer, np.uint8)
            img, ox, oy = post_processing(img.reshape((h, w, 4)) / 255.,
                                          self.dpi)
            image = fromarray(np.flipud(img), 1)
#            image.flipud_out()

            gc = self.new_gc()
            self._renderer.draw_image(gc,
                                      l+ox, height - b - h +oy,
                                      image)
Example #8
0
    def stop_filter(self, post_processing):
        """
        Save the plot in the current canvas as a image and apply
        the *post_processing* function.

           def post_processing(image, dpi):
             # ny, nx, depth = image.shape
             # image (numpy array) has RGBA channels and has a depth of 4.
             ...
             # create a new_image (numpy array of 4 channels, size can be
             # different). The resulting image may have offsets from
             # lower-left corner of the original image
             return new_image, offset_x, offset_y

        The saved renderer is restored and the returned image from
        post_processing is plotted (using draw_image) on it.
        """

        # WARNING.
        # For agg_filter to work, the rendere's method need
        # to overridden in the class. See draw_markers, and draw_path_collections

        from matplotlib._image import fromarray

        width, height = int(self.width), int(self.height)

        buffer, bounds = self._renderer.tostring_rgba_minimized()

        l, b, w, h = bounds


        self._renderer = self._filter_renderers.pop()
        self._update_methods()

        if w > 0 and h > 0:
            img = np.fromstring(buffer, np.uint8)
            img, ox, oy = post_processing(img.reshape((h, w, 4)) / 255.,
                                          self.dpi)
            image = fromarray(img, 1)
            image.flipud_out()

            gc = self.new_gc()
            self._renderer.draw_image(gc,
                                      l+ox, height - b - h +oy,
                                      image)
Example #9
0
    def make_image(self, magnification=1.0):
        # had to introduce argument magnification to satisfy the unit test
        # figimage_demo.py. I have no idea, how magnification should be used
        # within the function. It should be !=1.0 only for non-default DPI
        # settings in the PS backend, as introduced by patch #1562394
        # Probably Nicholas Young should look over this code and see, how
        # magnification should be handled correctly.
        if self._A is None:
            raise RuntimeError("You must first set the image array")

        x = self.to_rgba(self._A, self._alpha)

        im = _image.fromarray(x, 1)
        fc = self.figure.get_facecolor()
        im.set_bg(*mcolors.colorConverter.to_rgba(fc, 0))
        im.is_grayscale = self.cmap.name == "gray" and len(self._A.shape) == 2
        if self.origin == "upper":
            im.flipud_out()

        return im
Example #10
0
    def make_image(self, magnification=1.0):
        # had to introduce argument magnification to satisfy the unit test
        # figimage_demo.py. I have no idea, how magnification should be used
        # within the function. It should be !=1.0 only for non-default DPI
        # settings in the PS backend, as introduced by patch #1562394
        # Probably Nicholas Young should look over this code and see, how
        # magnification should be handled correctly.
        if self._A is None:
            raise RuntimeError('You must first set the image array')

        x = self.to_rgba(self._A, self._alpha)

        im = _image.fromarray(x, 1)
        fc = self.figure.get_facecolor()
        im.set_bg(*mcolors.colorConverter.to_rgba(fc, 0))
        im.is_grayscale = (self.cmap.name == "gray"
                           and len(self._A.shape) == 2)
        if self.origin == 'upper':
            im.flipud_out()

        return im
Example #11
0
    def draw_tex(self, gc, x, y, s, prop, angle):
        # todo, handle props, angle, origins
        rgb = gc.get_rgb()
        size = prop.get_size_in_points()
        dpi = self.dpi.get()

        flip = angle==90
        w,h,d = self.get_text_width_height_descent(s, prop, 'TeX', rgb)
        if flip:
            w,h = h,w
            x -= w

        texmanager = self.get_texmanager()
        key = s, size, dpi, rgb, angle, texmanager.get_font_config()
        im = self.texd.get(key)
        if im is None:
            Z = texmanager.get_rgba(s, size, dpi, rgb)
            if flip:
                r = Z[:,:,0]
                g = Z[:,:,1]
                b = Z[:,:,2]
                a = Z[:,:,3]
                m,n,tmp = Z.shape

                def func(x):
                    return npy.transpose(npy.fliplr(x))

                Z = npy.zeros((n,m,4), float)
                Z[:,:,0] = func(r)
                Z[:,:,1] = func(g)
                Z[:,:,2] = func(b)
                Z[:,:,3] = func(a)
            im = fromarray(Z, 1)
            im.flipud_out()
            self.texd[key] = im

        cliprect = gc.get_clip_rectangle()
        if cliprect is None: bbox = None
        else: bbox = lbwh_to_bbox(*cliprect)
        self.draw_image(x, self.height-y, im, bbox)
Example #12
0
    def draw_tex(self, gc, x, y, s, prop, angle):
        # todo, handle props, angle, origins
        rgb = gc.get_rgb()
        size = prop.get_size_in_points()
        dpi = self.dpi.get()

        flip = angle == 90
        w, h = self.get_text_width_height(s, prop, 'TeX', rgb)
        if flip:
            w, h = h, w
            x -= w

        texmanager = self.get_texmanager()
        key = s, size, dpi, rgb, angle, texmanager.get_font_config()
        im = self.texd.get(key)
        if im is None:
            Z = texmanager.get_rgba(s, size, dpi, rgb)
            if flip:
                r = Z[:, :, 0]
                g = Z[:, :, 1]
                b = Z[:, :, 2]
                a = Z[:, :, 3]
                m, n, tmp = Z.shape

                def func(x):
                    return transpose(fliplr(x))

                Z = zeros((n, m, 4), typecode=Float)
                Z[:, :, 0] = func(r)
                Z[:, :, 1] = func(g)
                Z[:, :, 2] = func(b)
                Z[:, :, 3] = func(a)
            im = fromarray(Z, 1)
            im.flipud_out()
            self.texd[key] = im

        cliprect = gc.get_clip_rectangle()
        if cliprect is None: bbox = None
        else: bbox = lbwh_to_bbox(*cliprect)
        self.draw_image(x, self.height - y, im, bbox)
Example #13
0
    def draw_tex(self, gc, x, y, s, prop, angle):
        # todo, handle props, angle, origins
        rgb = gc.get_rgb()
        size = prop.get_size_in_points()
        dpi = self.dpi.get()

        flip = angle == 90
        w, h = self.get_text_width_height(s, prop, 'TeX', rgb)
        if flip:
            w, h = h, w
            x -= w

        key = s, size, dpi, rgb
        im = self.texd.get(key)

        if im is None:
            Z = self.texmanager.get_rgba(s, size, dpi, rgb)
            if flip:
                r = Z[:, :, 0]
                g = Z[:, :, 1]
                b = Z[:, :, 2]
                a = Z[:, :, 3]
                m, n, tmp = Z.shape

                def func(x):
                    return transpose(fliplr(x))

                Z = zeros((n, m, 4), typecode=Float)
                Z[:, :, 0] = func(r)
                Z[:, :, 1] = func(g)
                Z[:, :, 2] = func(b)
                Z[:, :, 3] = func(a)
            im = fromarray(Z, 1)
            self.texd[key] = im

        self.draw_image(x, y - h, im, 'upper', self.bbox)
Example #14
0
    def draw_tex(self, gc, x, y, s, prop, angle):
        # todo, handle props, angle, origins
        rgb = gc.get_rgb()
        size = prop.get_size_in_points()
        dpi = self.dpi.get()

        flip = angle==90
        w,h = self.get_text_width_height(s, prop, 'TeX', rgb)
        if flip:
            w,h = h,w
            x -= w

        key = s, size, dpi, rgb
        im = self.texd.get(key)
        
        if im is None:
            Z = self.texmanager.get_rgba(s, size, dpi, rgb)
            if flip:
                r = Z[:,:,0]
                g = Z[:,:,1]
                b = Z[:,:,2]
                a = Z[:,:,3]
                m,n,tmp = Z.shape

                def func(x):
                    return transpose(fliplr(x))

                Z = zeros((n,m,4), typecode=Float)
                Z[:,:,0] = func(r)
                Z[:,:,1] = func(g)
                Z[:,:,2] = func(b)
                Z[:,:,3] = func(a)
            im = fromarray(Z, 1)
            self.texd[key] = im            

        self.draw_image(x, y-h, im, 'upper', self.bbox)
Example #15
0
 def _get_unsampled_image(self, A, image_extents, viewlim):
     """
     convert numpy array A with given extents ([x1, x2, y1, y2] in
     data coordinate) into the Image, given the vielim (should be a
     bbox instance).  Image will be clipped if the extents is
     significantly larger than the viewlim.
     """
     xmin, xmax, ymin, ymax = image_extents
     dxintv = xmax-xmin
     dyintv = ymax-ymin
     sx = dxintv/viewlim.width
     sy = dyintv/viewlim.height
     numrows, numcols = A.shape[:2]
     if sx > 2:
         x0 = (viewlim.x0-xmin)/dxintv * numcols
         ix0 = max(0, int(x0 - self._filterrad))
         x1 = (viewlim.x1-xmin)/dxintv * numcols
         ix1 = min(numcols, int(x1 + self._filterrad))
         xslice = slice(ix0, ix1)
         xmin_old = xmin
         xmin = xmin_old + ix0*dxintv/numcols
         xmax = xmin_old + ix1*dxintv/numcols
         dxintv = xmax - xmin
         sx = dxintv/viewlim.width
     else:
         xslice = slice(0, numcols)
     if sy > 2:
         y0 = (viewlim.y0-ymin)/dyintv * numrows
         iy0 = max(0, int(y0 - self._filterrad))
         y1 = (viewlim.y1-ymin)/dyintv * numrows
         iy1 = min(numrows, int(y1 + self._filterrad))
         if self.origin == 'upper':
             yslice = slice(numrows-iy1, numrows-iy0)
         else:
             yslice = slice(iy0, iy1)
         ymin_old = ymin
         ymin = ymin_old + iy0*dyintv/numrows
         ymax = ymin_old + iy1*dyintv/numrows
         dyintv = ymax - ymin
         sy = dyintv/viewlim.height
     else:
         yslice = slice(0, numrows)
     if xslice != self._oldxslice or yslice != self._oldyslice:
         self._imcache = None
         self._oldxslice = xslice
         self._oldyslice = yslice
     if self._imcache is None:
         if self._A.dtype == np.uint8 and len(self._A.shape) == 3:
             im = _image.frombyte(self._A[yslice,xslice,:], 0)
             im.is_grayscale = False
         else:
             if self._rgbacache is None:
                 x = self.to_rgba(self._A, self._alpha)
                 self._rgbacache = x
             else:
                 x = self._rgbacache
             im = _image.fromarray(x[yslice,xslice], 0)
             if len(self._A.shape) == 2:
                 im.is_grayscale = self.cmap.is_gray()
             else:
                 im.is_grayscale = False
         self._imcache = im
         if self.origin=='upper':
             im.flipud_in()
     else:
         im = self._imcache
     return im, xmin, ymin, dxintv, dyintv, sx, sy
Example #16
0
    def _get_unsampled_image(self, A, image_extents, viewlim):
        """
        convert numpy array A with given extents ([x1, x2, y1, y2] in
        data coordinate) into the Image, given the vielim (should be a
        bbox instance).  Image will be clipped if the extents is
        significantly larger than the viewlim.
        """
        xmin, xmax, ymin, ymax = image_extents
        dxintv = xmax - xmin
        dyintv = ymax - ymin

        # the viewport scale factor
        if viewlim.width == 0.0 and dxintv == 0.0:
            sx = 1.0
        else:
            sx = dxintv / viewlim.width
        if viewlim.height == 0.0 and dyintv == 0.0:
            sy = 1.0
        else:
            sy = dyintv / viewlim.height
        numrows, numcols = A.shape[:2]
        if sx > 2:
            x0 = (viewlim.x0 - xmin) / dxintv * numcols
            ix0 = max(0, int(x0 - self._filterrad))
            x1 = (viewlim.x1 - xmin) / dxintv * numcols
            ix1 = min(numcols, int(x1 + self._filterrad))
            xslice = slice(ix0, ix1)
            xmin_old = xmin
            xmin = xmin_old + ix0 * dxintv / numcols
            xmax = xmin_old + ix1 * dxintv / numcols
            dxintv = xmax - xmin
            sx = dxintv / viewlim.width
        else:
            xslice = slice(0, numcols)

        if sy > 2:
            y0 = (viewlim.y0 - ymin) / dyintv * numrows
            iy0 = max(0, int(y0 - self._filterrad))
            y1 = (viewlim.y1 - ymin) / dyintv * numrows
            iy1 = min(numrows, int(y1 + self._filterrad))
            if self.origin == 'upper':
                yslice = slice(numrows - iy1, numrows - iy0)
            else:
                yslice = slice(iy0, iy1)
            ymin_old = ymin
            ymin = ymin_old + iy0 * dyintv / numrows
            ymax = ymin_old + iy1 * dyintv / numrows
            dyintv = ymax - ymin
            sy = dyintv / viewlim.height
        else:
            yslice = slice(0, numrows)

        if xslice != self._oldxslice or yslice != self._oldyslice:
            self._imcache = None
            self._oldxslice = xslice
            self._oldyslice = yslice

        if self._imcache is None:
            if self._A.dtype == np.uint8 and len(self._A.shape) == 3:
                im = _image.frombyte(self._A[yslice, xslice, :], 0)
                im.is_grayscale = False
            else:
                if self._rgbacache is None:
                    x = self.to_rgba(self._A, self._alpha)
                    self._rgbacache = x
                else:
                    x = self._rgbacache
                im = _image.fromarray(x[yslice, xslice], 0)
                if len(self._A.shape) == 2:
                    im.is_grayscale = self.cmap.is_gray()
                else:
                    im.is_grayscale = False
            self._imcache = im

            if self.origin == 'upper':
                im.flipud_in()
        else:
            im = self._imcache

        return im, xmin, ymin, dxintv, dyintv, sx, sy
Example #17
0
    def make_image(self, magnification=1.0):
        if self._A is None:
            raise RuntimeError('You must first set the image array or the image attribute')

        xmin, xmax, ymin, ymax = self.get_extent()
        dxintv = xmax-xmin
        dyintv = ymax-ymin

        # the viewport scale factor
        sx = dxintv/self.axes.viewLim.width
        sy = dyintv/self.axes.viewLim.height
        numrows, numcols = self._A.shape[:2]
        if sx > 2:
            x0 = (self.axes.viewLim.x0-xmin)/dxintv * numcols
            ix0 = max(0, int(x0 - self._filterrad))
            x1 = (self.axes.viewLim.x1-xmin)/dxintv * numcols
            ix1 = min(numcols, int(x1 + self._filterrad))
            xslice = slice(ix0, ix1)
            xmin_old = xmin
            xmin = xmin_old + ix0*dxintv/numcols
            xmax = xmin_old + ix1*dxintv/numcols
            dxintv = xmax - xmin
            sx = dxintv/self.axes.viewLim.width
        else:
            xslice = slice(0, numcols)

        if sy > 2:
            y0 = (self.axes.viewLim.y0-ymin)/dyintv * numrows
            iy0 = max(0, int(y0 - self._filterrad))
            y1 = (self.axes.viewLim.y1-ymin)/dyintv * numrows
            iy1 = min(numrows, int(y1 + self._filterrad))
            if self.origin == 'upper':
                yslice = slice(numrows-iy1, numrows-iy0)
            else:
                yslice = slice(iy0, iy1)
            ymin_old = ymin
            ymin = ymin_old + iy0*dyintv/numrows
            ymax = ymin_old + iy1*dyintv/numrows
            dyintv = ymax - ymin
            sy = dyintv/self.axes.viewLim.height
        else:
            yslice = slice(0, numrows)

        if xslice != self._oldxslice or yslice != self._oldyslice:
            self._imcache = None
            self._oldxslice = xslice
            self._oldyslice = yslice

        if self._imcache is None:
            if self._A.dtype == np.uint8 and len(self._A.shape) == 3:
                im = _image.frombyte(self._A[yslice,xslice,:], 0)
                im.is_grayscale = False
            else:
                if self._rgbacache is None:
                    x = self.to_rgba(self._A, self._alpha)
                    self._rgbacache = x
                else:
                    x = self._rgbacache
                im = _image.fromarray(x[yslice,xslice], 0)
                if len(self._A.shape) == 2:
                    im.is_grayscale = self.cmap.is_gray()
                else:
                    im.is_grayscale = False
            self._imcache = im

            if self.origin=='upper':
                im.flipud_in()
        else:
            im = self._imcache

        fc = self.axes.get_frame().get_facecolor()
        bg = mcolors.colorConverter.to_rgba(fc, 0)
        im.set_bg( *bg)

        # image input dimensions
        im.reset_matrix()
        numrows, numcols = im.get_size()

        im.set_interpolation(self._interpd[self._interpolation])

        im.set_resample(self._resample)

        # the viewport translation
        tx = (xmin-self.axes.viewLim.x0)/dxintv * numcols
        ty = (ymin-self.axes.viewLim.y0)/dyintv * numrows

        l, b, r, t = self.axes.bbox.extents
        widthDisplay = (round(r) + 0.5) - (round(l) - 0.5)
        heightDisplay = (round(t) + 0.5) - (round(b) - 0.5)
        widthDisplay *= magnification
        heightDisplay *= magnification
        im.apply_translation(tx, ty)

        # resize viewport to display
        rx = widthDisplay / numcols
        ry = heightDisplay  / numrows
        im.apply_scaling(rx*sx, ry*sy)
        im.resize(int(widthDisplay+0.5), int(heightDisplay+0.5),
                  norm=self._filternorm, radius=self._filterrad)
        return im
Example #18
0
    def make_image(self, magnification=1.0):
        if self._A is None:
            raise RuntimeError('You must first set the image array or the image attribute')

        xmin, xmax, ymin, ymax = self.get_extent()
        dxintv = xmax-xmin
        dyintv = ymax-ymin

        # the viewport scale factor
        sx = dxintv/self.axes.viewLim.width
        sy = dyintv/self.axes.viewLim.height
        numrows, numcols = self._A.shape[:2]
        if sx > 2:
            x0 = (self.axes.viewLim.x0-xmin)/dxintv * numcols
            ix0 = max(0, int(x0 - self._filterrad))
            x1 = (self.axes.viewLim.x1-xmin)/dxintv * numcols
            ix1 = min(numcols, int(x1 + self._filterrad))
            xslice = slice(ix0, ix1)
            xmin_old = xmin
            xmin = xmin_old + ix0*dxintv/numcols
            xmax = xmin_old + ix1*dxintv/numcols
            dxintv = xmax - xmin
            sx = dxintv/self.axes.viewLim.width
        else:
            xslice = slice(0, numcols)

        if sy > 2:
            y0 = (self.axes.viewLim.y0-ymin)/dyintv * numrows
            iy0 = max(0, int(y0 - self._filterrad))
            y1 = (self.axes.viewLim.y1-ymin)/dyintv * numrows
            iy1 = min(numrows, int(y1 + self._filterrad))
            if self.origin == 'upper':
                yslice = slice(numrows-iy1, numrows-iy0)
            else:
                yslice = slice(iy0, iy1)
            ymin_old = ymin
            ymin = ymin_old + iy0*dyintv/numrows
            ymax = ymin_old + iy1*dyintv/numrows
            dyintv = ymax - ymin
            sy = dyintv/self.axes.viewLim.height
        else:
            yslice = slice(0, numrows)

        if xslice != self._oldxslice or yslice != self._oldyslice:
            self._imcache = None
            self._oldxslice = xslice
            self._oldyslice = yslice

        if self._imcache is None:
            if self._A.dtype == np.uint8 and len(self._A.shape) == 3:
                im = _image.frombyte(self._A[yslice,xslice,:], 0)
                im.is_grayscale = False
            else:
                if self._rgbacache is None:
                    x = self.to_rgba(self._A, self._alpha)
                    self._rgbacache = x
                else:
                    x = self._rgbacache
                im = _image.fromarray(x[yslice,xslice], 0)
                if len(self._A.shape) == 2:
                    im.is_grayscale = self.cmap.is_gray()
                else:
                    im.is_grayscale = False
            self._imcache = im

            if self.origin=='upper':
                im.flipud_in()
        else:
            im = self._imcache

        fc = self.axes.patch.get_facecolor()
        bg = mcolors.colorConverter.to_rgba(fc, 0)
        im.set_bg( *bg)

        # image input dimensions
        im.reset_matrix()
        numrows, numcols = im.get_size()
        if numrows < 1 or numcols < 1:   # out of range
            return None
        im.set_interpolation(self._interpd[self._interpolation])

        im.set_resample(self._resample)

        # the viewport translation
        tx = (xmin-self.axes.viewLim.x0)/dxintv * numcols
        ty = (ymin-self.axes.viewLim.y0)/dyintv * numrows

        l, b, r, t = self.axes.bbox.extents
        widthDisplay = (round(r*magnification) + 0.5) - (round(l*magnification) - 0.5)
        heightDisplay = (round(t*magnification) + 0.5) - (round(b*magnification) - 0.5)
        im.apply_translation(tx, ty)

        # resize viewport to display
        rx = widthDisplay / numcols
        ry = heightDisplay  / numrows
        im.apply_scaling(rx*sx, ry*sy)
        im.resize(int(widthDisplay+0.5), int(heightDisplay+0.5),
                  norm=self._filternorm, radius=self._filterrad)
        return im
Example #19
0
    def make_image(self, magnification=1.0):
        if self._A is None:
            raise RuntimeError(
                'You must first set the image array or the image attribute')

        if self._imcache is None:
            if self._A.dtype == npy.uint8 and len(self._A.shape) == 3:
                im = _image.frombyte(self._A, 0)
                im.is_grayscale = False
            else:
                x = self.to_rgba(self._A, self._alpha)
                im = _image.fromarray(x, 0)
                if len(self._A.shape) == 2:
                    im.is_grayscale = self.cmap.is_gray()
                else:
                    im.is_grayscale = False
            self._imcache = im

            if self.origin == 'upper':
                im.flipud_in()
        else:
            im = self._imcache

        fc = self.axes.get_frame().get_facecolor()
        bg = mcolors.colorConverter.to_rgba(fc, 0)
        im.set_bg(*bg)

        # image input dimensions
        im.reset_matrix()
        numrows, numcols = im.get_size()

        im.set_interpolation(self._interpd[self._interpolation])

        xmin, xmax, ymin, ymax = self.get_extent()
        dxintv = xmax - xmin
        dyintv = ymax - ymin

        # the viewport scale factor
        sx = dxintv / self.axes.viewLim.width()
        sy = dyintv / self.axes.viewLim.height()

        if im.get_interpolation() != _image.NEAREST:
            im.apply_translation(-1, -1)

        # the viewport translation
        tx = (xmin - self.axes.viewLim.xmin()) / dxintv * numcols
        ty = (ymin - self.axes.viewLim.ymin()) / dyintv * numrows

        l, b, widthDisplay, heightDisplay = self.axes.bbox.get_bounds()
        widthDisplay *= magnification
        heightDisplay *= magnification

        im.apply_translation(tx, ty)
        im.apply_scaling(sx, sy)

        # resize viewport to display
        rx = widthDisplay / numcols
        ry = heightDisplay / numrows
        im.apply_scaling(rx, ry)

        im.resize(int(widthDisplay + 0.5),
                  int(heightDisplay + 0.5),
                  norm=self._filternorm,
                  radius=self._filterrad)

        return im