''' PMGraphics.py Functions to permit using AWT graphics in PixelMath. The functions here allow and demonstrate how to draw directly onto PixelMath images from Python programs by taking advantage of Jython's integration with Java. There are three parts to the process: 1. Getting a Graphics object from a PixelMath Image Frame. The function getPMGraphics makes this easy. 2. Drawing with the Graphics object g. This is simply a matter of using Java methods and classes, with Python syntax. 3. Updating the Image Frame so the numbers are consistent with the image. The function updatePMImageNumbers makes this easy. ''' from java.awt import Color, Font from java.awt.image import BufferedImage from JythonPixelMathInterface import * G = None WN = None FONTSIZE = 18 FONT = Font("Helvetica", 2, FONTSIZE) X=50 Y=50 COLOR=Color(127,0,192) LASTY=0 def getPMGraphics(wn=1): from pmpython.pixelmath import CalcPane tcp = CalcPane.theCalcPane imf = tcp.getImageFrame(wn) try: g = imf.theImage.getGraphics() except: w = imf.theImage.getWidth() h = imf.theImage.getHeight() bi = BufferedImage(w, h, BufferedImage.TYPE_INT_RGB) g = bi.getGraphics() g.drawImage(imf.theImage, 0, 0, None) imf.theImage = bi global G, WN G = g; WN = wn return g # After drawing on the Java image belonging to a # PixelMath Image Frame, you should call the following # to make sure that the numbers (stored separately) that # correspond to the image are updated to be consistent # with the image. def updatePMImageNumbers(wn=1): from pmpython.pixelmath import CalcPane imf = CalcPane.theCalcPane.getImageFrame(wn) imf.getImageCanvas().recalcPixels() # The following is a convenient way to draw text into a # PixelMath image. The string must always be specified. # The other arguments are optional and if not specified, # you get either the default or what was specified most recently. # In the case of y not specified, you get the last value of # y plus an increment that is 1.7 times the FONTSIZE. def drawStringInPMImage(theStr, wn=-1, fontSize=-1, x=-1, y=-1, color=None): global G, WN, FONTSIZE, FONT, X, Y, COLOR, LASTY if (wn>0 and not isValidWindow(wn)) or\ (wn==0 or (wn==-1 and not isValidWindow(WN))): if wn>0: WN=wn WN=pmNewImage(0,"PixelMath Image Frame", 800, 600, 0, 0, 0) pmMaximizeWindow(WN) G = getPMGraphics(WN) if fontSize > -1: FONT = Font("Helvetica", 1, fontSize) G.setFont(FONT) fm = G.getFontMetrics() sw = fm.stringWidth(theStr) imageW = pmGetImageWidth(WN) imageH = pmGetImageHeight(WN) if x==-1: # find x that centers the string in the window. x = int((imageW - sw)/2) if y==-1: y = LASTY + int(1.7*FONTSIZE) LASTY = y if color!=None: COLOR = Color(color[0],color[1],color[2]) G.setColor(COLOR) G.drawString(theStr, x, y) updatePMImageNumbers(WN) def isValidWindow(wn): vwn = pmGetValidWindowNumbers() return wn in vwn def test1(): drawStringInPMImage("Imperative Programming",\ wn=0, fontSize=24, y=200, color=(128,0,255)) drawStringInPMImage("is") drawStringInPMImage("pretty basic.") def test2(): pmNewImage(1, "A PixelMath Image Window", 600, 400, 128, 0, 192) g = getPMGraphics(1) g.setColor(Color(0, 0, 255)) timesFont = Font("Helvetica", 1, 24) g.setFont(timesFont) g.drawString("Text on an Image", 250, 200) updatePMImageNumbers(1) if __name__ == "__main__": test1()