def __init__(self, container, data2d, levels, matrix=None, resize=None, datamin=None, datamax=None, fill=False, **kwargs): self.data2d = numpy.array(data2d) if datamax is None: datamax = self.data2d.max() if datamin is None: datamin = self.data2d.min() if hasattr(levels, "__getitem__"): self.levels = levels else: noLevels = levels #print noLevels levels = (numpy.arange(noLevels)+1.)/(noLevels+1) #print levels levels = levels*(datamax-datamin)+datamin #print levels, self.data2d.min(), self.data2d.max() self.levels = levels self.matrix = matrix if self.matrix is None: self.matrix = kaplot.Matrix() self.fill = fill self.resize = resize if resize is not None: height,width = data2d.shape (x1, y1), (x2, y2) = resize #print "~~~", resize, width, height #kaplot.Matrix.translate(-0.5, -0.5) *\ self.matrix = \ kaplot.Matrix.translate(x1, y1) *\ kaplot.Matrix.scale((x2-x1)/float(width-1), (y2-y1)/float(height-1)) *\ kaplot.Matrix.translate(-1.0, -1.0) #kaplot.Matrix.scale((x2-x1)/float(width-1), (y2-y1)/float(height-1)) PlotObject.__init__(self, container, **kwargs)
def __init__(self, container, data2d, matrix=None, mask2d=None, colormap="rainbow", datamin=None, datamax=None, function="linear", resize=None, context=None, **kwargs): self.data2d = numpy.array(data2d) if matrix is None: matrix = kaplot.Matrix() self.matrix = matrix # if resize: # height, width = self.data2d.shape # height, width = float(height), float(width) # (x1, y1), (x2, y2) = resize # self.matrix = \ # kaplot.Matrix.translate(-0.5, -0.5) *\ # kaplot.Matrix.translate(float(x1), float(y1)) *\ # kaplot.Matrix.scale((x2-x1)/width, (y2-y1)/height) if resize is not None: height,width = data2d.shape (x1, y1), (x2, y2) = resize #print "~~~", resize, width, height # kaplot.Matrix.translate(-0.5, -0.5) *\ dx = (x2-x1)/float(width-1) dy = (y2-y1)/float(height-1) self.matrix = \ kaplot.Matrix.translate(float(x1), float(y1)) *\ kaplot.Matrix.scale(dx, dy) PlotObject.__init__(self, container, **kwargs) self.mask2d = mask2d self.colormap = colormap self.datamin = datamin self.datamax = datamax self.function = function if datamin == None: self.datamin = self.data2d.min() if datamax == None: self.datamax = self.data2d.max()
def getViewportMatrix(self): p1, p2 = self.viewportList[-1] return kaplot.Matrix.scalebox_inverse(p1, p2) (x1, y1), (x2, y2) = self.viewportList[-1] width = x2 - x1 height = y2 - y1 sx = width sy = height tx = x1 ty = y1 return kaplot.Matrix(sx, sy, tx, ty)
def getWorldMatrix(self): p1, p2 = self.worldList[-1] #return kaplot.Matrix.scalebox(p1, p2) (x1, y1), (x2, y2) = self.worldList[-1] width = x2 - x1 height = y2 - y1 sx = 1.0 / width sy = 1.0 / height tx = -x1 / float(width) ty = -y1 / float(height) mat = kaplot.Matrix(sx, sy, tx, ty) return mat
def _getMatrixParams(self, matrix=None): if matrix is None: matrix = kaplot.Matrix() m = self.getDeviceMatrix() * self.getViewportMatrix() * self.getWorldMatrix() * matrix return m.xx, m.yy, m.tx, m.ty m = m.matrix return m[0][0], m[1][1], m[0][2], m[1][2] (vx1, vy1), (vx2, vy2) = self.viewportList[-1] vwidth = vx2 - vx1 vheight = vy2 - vy1 (x1, y1), (x2, y2) = self.worldList[-1] width = x2 - x1 height = y2 - y1 sx = 1.0 / width * self.width * vwidth sy = 1.0 / height * self.height * vheight tx = (x1 / width + vx1)* self.width ty = (y1 / height + vy1) * self.height return sx, sy, tx, ty
def _setAggFillstyle(self): fillstyle = self._fillstyle if fillstyle == "fill": self.agg.clear_pattern() return elif fillstyle not in kaplot.patterns: raise ValueError, "unknown fillstyle %r" % fillstyle pattern = kaplot.patterns[fillstyle] patternSize = self._patternsize patternSize, patternUnits = kaplot.utils.splitDimension(patternSize) patternSizePixels = kaplot.utils.convertToPixels(patternSize, patternUnits, dpi=self.dpi) width, height = int(patternSizePixels+0.5), int(patternSizePixels+0.5) patternsurface = Agg(width, height) patternsurface.clear(0,0,0,0) r, g, b = self._color.getRGB() patternsurface.set_color(r, g, b) for part in range(pattern.parts): xlistlist, ylistlist = pattern.getXY(part) for xlist, ylist in zip(xlistlist, ylistlist): patternsurface.move_to(xlist[0]*width, ylist[0]*height) for xvalue, yvalue in zip(xlist[1:], ylist[1:]): patternsurface.line_to(xvalue*width, yvalue*height) if pattern.solid: patternsurface.fill() else: patternsurface.stroke() #matrix = kaplot.Matrix.rotate(pattern.angle) matrix = kaplot.Matrix() #self._setAggSurfaceMatrix(patternsurface, matrix) #kaplot.Matrix.rotate(pattern.angle) #Matrix(numpy.array([[, ,0],[, ,0], [0,0,1]], numpy.Float)) #sx, sy, tx, ty = matrix.xx, matrix.yy, matrix.tx, matrix.ty angle = pattern.angle patternsurface.set_matrix(math.cos(angle), -math.sin(angle), math.sin(angle), math.cos(angle), 0, 0) self._currentPattern = patternsurface self.agg.set_pattern(self._currentPattern)
import kaplot import kaplot.objects import kaplot.astro from numpy import * m1 = kaplot.Matrix(2, 3, 0, 0) m2 = kaplot.Matrix(1, 1, 10, 10) print m1 print m2 print m1 * m2 plot = kaplot.objects.Plot() page = kaplot.objects.Page(plot) box = kaplot.objects.Box(page, viewport=((0.1, 0.1), (0.9, 0.9))) line = kaplot.objects.Line(box, 1, 1, 9, 5) x = arange(0.01, 10, 0.1) y = sin(x) / x * 5 + 3 pline = kaplot.objects.PolyLine(box, x, y, color="green") pline = kaplot.objects.PolyLine(box, x, y * 0.9, color="green") sphere = kaplot.astro.WcsSphere(box) device = kaplot.device() device.draw(plot) device.close()
def drawIndexedImage(self, data2d, colormap, function="linear", mask2d=None, matrix=None, datamin=None, datamax=None): if matrix is None: matrix = kaplot.Matrix() #self._setMatrix() #self.agg.set_matrix(2, 0, 0, 2, tx, ty) #self._setIdentityMatrix() colormap = kaplot.utils.getColormap(colormap) function = kaplot.utils.getFunction(function) data2d = numpy.array(data2d, numpy.float) if datamin == None: datamin = data2d.min() if datamax == None: datamax = data2d.max() scale = 255.0 / (datamax - datamin) data2d = ((data2d - datamin) * scale) data2d = numpy.clip(data2d, 0, 255) height, width = data2d.shape if matrix is None: matrix = kaplot.Matrix() #m = self.getDeviceMatrix() * self.getViewportMatrix() * self.getWorldMatrix() *\ # kaplot.Matrix.translate(0.5, 0.5) * \ # matrix * kaplot.Matrix.translate(-0.5, -0.5) m = self.getDeviceMatrix() * self.getViewportMatrix() * self.getWorldMatrix() *\ matrix *\ kaplot.Matrix.translate(-1.0, -1.0) #kaplot.Matrix.translate(-1.0, -1.0) #kaplot.Matrix.translate(0.5, 0.5) * \ #matrix * kaplot.Matrix.translate(-0.5, -0.5) sx, sy, tx, ty = m.xx, m.yy, m.tx, m.ty #sx, sy, tx, ty = self._getMatrixParams(matrix * ) tx += 0.5 ty += 0.5 #print sx, sy, tx, ty # fixme, center small images dx = 0 dy = 0 self.agg.set_matrix(sx, 0, 0, sy, tx-dx, ty-dy) #print data2d.min(), data2d.max() #print data2d[0][0] palette = numpy.zeros((768,), numpy.int8) for i in range(0, 256): index = function(i/255.0) r, g, b = colormap(index).getRGB() palette[i*3+0] = r * 255 palette[i*3+1] = g * 255 palette[i*3+2] = b * 255 pilIndexedImage = PIL.Image.fromstring("P", (width,height), data2d.astype(numpy.uint8).tostring()) pilIndexedImage.putpalette(palette.tostring()) #pilIndexedImage.show() if mask2d != None: alpha = self._alpha * 0xff mask2d = numpy.where(mask2d >= 1, alpha, 0).astype(numpy.int8) maskdata = mask2d.astype(numpy.int8).tostring() maskImage = PIL.Image.fromstring("L", (width, height), maskdata) pilRgbImage = pilIndexedImage.convert("RGBA") pilRgbImage.putalpha(maskImage) #elif self._alpha < 1: # #alpha = self._alpha * 0xff # #mask2d = numpy.where(mask2d >= 1, alpha, 0).astype(numpy.int8) # alphadata = ((numpy.zeros((width,height)) + self._alpha)*255).astype(numpy.int8).tostring() # alphaImage = PIL.Image.fromstring("L", (width, height), alphadata) # pilRgbImage = pilIndexedImage.convert("RGBA") # pilRgbImage.putalpha(alphaImage) else: pilRgbImage = pilIndexedImage.convert("RGBA") #pilRgbImage.show() datastring = pilRgbImage.tostring() self._drawRbgData(datastring, width, height)