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: A = self._A if self.origin == 'upper': A = A[::-1] if A.dtype == np.uint8 and len(A.shape) == 3: im = _image.frombyte(A, 0) im.is_grayscale = False else: if self._rgbacache is None: x = self.to_rgba(A, bytes=True) self._rgbacache = x else: x = self._rgbacache im = _image.frombyte(x, 0) if len(A.shape) == 2: im.is_grayscale = self.cmap.is_gray() else: im.is_grayscale = False self._imcache = im 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 = abs(round(r) - round(l)) heightDisplay = abs(round(t) - round(b)) widthDisplay *= magnification heightDisplay *= magnification numrows, numcols = self._A.shape[:2] if (not self.interp_at_native and widthDisplay == numcols and heightDisplay == numrows): im.set_interpolation(0) # 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
def finish_gl_drawing(glcanvas, renderer, tag, trans): renderer._k_globj += 1 if (renderer._k_globj != renderer._num_globj): return if not glcanvas._hittest_map_update: glcanvas._no_hl = False id_dict = glcanvas.draw_mpl_artists(tag) im = glcanvas.read_data(tag) gc = renderer.new_gc() x, y = trans.transform(frame_range[0:2]) im = frombyte(im, 1) if not isMPL2: im.is_grayscale = False # this is needed to print in MPL1.5 renderer.draw_image(gc, round(x), round(y), im) gc.restore() else: # # need to draw twice due to buffering of pixel reading # glcanvas._no_hl = True glcanvas._hittest_map_update = True id_dict = glcanvas.draw_mpl_artists(tag) # im : image, im2, im2d: id, im3: depth im, im2, im2d, im3 = glcanvas.stored_im glcanvas._hittest_map_update = False id_dict = glcanvas.draw_mpl_artists(tag) im = glcanvas.read_data(tag) glcanvas._hittest_map_update = True gc = renderer.new_gc() x, y = trans.transform(frame_range[0:2]) im = frombyte(im, 1) if not isMPL2: im.is_grayscale = False # this is needed to print in MPL1.5 if renderer.gl_svg_rescale: # svg renderer has image_dpi = 100 (not 72) # therefore width and height needs to be set x2, y2 = trans.transform(frame_range[2:]) renderer.draw_image(gc, round(x), round(y), im, dx=round(x2 - x), dy=round(y2 - y)) else: renderer.draw_image(gc, round(x), round(y), im) renderer.update_id_data((x, y, id_dict, im2, im2d, im3), tag=tag) gc.restore() tag._gl_img = im
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, bytes=True) self._rgbacache = x else: x = self._rgbacache im = _image.frombyte(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
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, bytes=True) 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.frombyte(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
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
def finish_gl_drawing(glcanvas, renderer, tag, trans): renderer._k_globj += 1 if (renderer._k_globj != renderer._num_globj): return #print ("finish gl draw", renderer._k_globj, renderer._num_globj) if not glcanvas._hittest_map_update: glcanvas._no_hl = False id_dict = glcanvas.draw_mpl_artists(tag) im = glcanvas.read_data(tag) # im : image, im2: id, im3: depth gc = renderer.new_gc() x, y =trans.transform(frame_range[0:2]) im = frombyte(im, 1) if not isMPL2: im.is_grayscale = False ## this is needed to print in MPL1.5 renderer.draw_image(gc, round(x), round(y), im) gc.restore() else: # # need to draw twice due to buffering of pixel reading # glcanvas._no_hl = True glcanvas._hittest_map_update = True id_dict = glcanvas.draw_mpl_artists(tag) # im : image, im2, im2d: id, im3: depth im, im2, im2d, im3 = glcanvas.read_data(tag) glcanvas._hittest_map_update = False id_dict = glcanvas.draw_mpl_artists(tag) im = glcanvas.read_data(tag) # im : image, im2: id, im3: depth glcanvas._hittest_map_update = True gc = renderer.new_gc() x, y =trans.transform(frame_range[0:2]) im = frombyte(im, 1) if not isMPL2: im.is_grayscale = False ## this is needed to print in MPL1.5 if renderer.gl_svg_rescale: ### svg renderer has image_dpi = 100 (not 72) ### therefore width and height needs to be set x2, y2 =trans.transform(frame_range[2:]) renderer.draw_image(gc, round(x), round(y), im, dx=round(x2-x), dy = round(y2-y)) else: renderer.draw_image(gc, round(x), round(y), im) renderer.update_id_data((x, y, id_dict, im2, im2d, im3), tag = tag) gc.restore() tag._gl_img = im
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 viewlim (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 self._A.ndim == 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, bytes=True) self._rgbacache = x else: x = self._rgbacache im = _image.frombyte(x[yslice,xslice,:], 0) if self._A.ndim == 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
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 viewlim (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)) 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: A = self._A if self.origin == 'upper': A = A[::-1] if A.dtype == np.uint8 and A.ndim == 3: im = _image.frombyte(A[yslice, xslice, :], 0) im.is_grayscale = False else: if self._rgbacache is None: x = self.to_rgba(A, bytes=False) # Avoid side effects: to_rgba can return its argument # unchanged. if np.may_share_memory(x, A): x = x.copy() # premultiply the colors x[..., 0:3] *= x[..., 3:4] x = (x * 255).astype(np.uint8) self._rgbacache = x else: x = self._rgbacache im = _image.frombyte(x[yslice, xslice, :], 0) if self._A.ndim == 2: im.is_grayscale = self.cmap.is_gray() else: im.is_grayscale = False self._imcache = im else: im = self._imcache return im, xmin, ymin, dxintv, dyintv, sx, sy
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
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
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
def _byte2image(self, img): image = frombyte(np.flipud(img), 1) #image.flipud_out() return image