Package omero :: Package util :: Module ROIDrawingUtils
[hide private]
[frames] | no frames]

Source Code for Module omero.util.ROIDrawingUtils

  1  # 
  2  # 
  3  #------------------------------------------------------------------------------ 
  4  #  Copyright (C) 2006-2009 University of Dundee. All rights reserved. 
  5  # 
  6  # 
  7  #       This program is free software; you can redistribute it and/or modify 
  8  #  it under the terms of the GNU General Public License as published by 
  9  #  the Free Software Foundation; either version 2 of the License, or 
 10  #  (at your option) any later version. 
 11  #  This program is distributed in the hope that it will be useful, 
 12  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  #  GNU General Public License for more details. 
 15  # 
 16  #  You should have received a copy of the GNU General Public License along 
 17  #  with this program; if not, write to the Free Software Foundation, Inc., 
 18  #  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 19  # 
 20  #------------------------------------------------------------------------------ 
 21   
 22  ### 
 23  # 
 24  # ROIDrawingCanvas draws the shapes from the obejct visited.  
 25  # These map to the ROI types in omero.model.*  
 26  # 
 27  # @author  Jean-Marie Burel      
 28  #       <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a> 
 29  # @author       Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp; 
 30  #       <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a> 
 31  # @version 3.0 
 32  # <small> 
 33  # (<b>Internal version:</b> $Revision: $Date: $) 
 34  # </small> 
 35  # @since 3.0-Beta4 
 36  # 
 37   
 38  """ 
 39   
 40  Example code to draw 10 ellipses randomly on an image. 
 41   
 42  import ROI_utils; 
 43  import ROIDrawingUtils; 
 44  from random import randint; 
 45   
 46  l = []; 
 47  for i in range(0,10): 
 48      e = ROI_utils.EllipseData(ROI_utils.ROICoordinate(), randint(100, 300), randint(100, 300), randint(20, 50), randint(20, 50)) 
 49      l.append(e); 
 50  d = ROIDrawingUtils.DrawingCanvas(); 
 51  d.createImage(400,400) 
 52  v = d.drawElements(l); 
 53  d.image.show()  
 54   
 55   
 56  Example code to draw a polyline on an image an display it in PIL. 
 57   
 58  try: 
 59      from PIL import Image, ImageDraw # see ticket:2597 
 60  except ImportError: 
 61      import Image, ImageDraw # see ticket:2597 
 62   
 63  import ROI_utils 
 64  import ROIDrawingUtils 
 65   
 66  drawingCanvas = ROIDrawingUtils.DrawingCanvas(); 
 67  points = [10,30, 40, 80, 100, 150] 
 68  polygonData = ROI_utils.PolylineData(ROI_utils.ROICoordinate(), points); 
 69  drawingCanvas.createImage(400,400); 
 70  drawingCanvas.drawElements([polygonData]); 
 71  drawingCanvas.image.show()  
 72   
 73   
 74  """ 
 75   
 76   
 77  try: 
 78      from PIL import Image, ImageDraw # see ticket:2597 
 79  except ImportError: 
 80      import Image, ImageDraw # see ticket:2597 
 81   
 82   
 83  ## 
 84  # Drawing canvas allows the creation of shapes on an 
 85  # image using PIL, the class can be supplied with an 
 86  # image and will write on that or can create an image. 
 87  # The object will also visit a list of objects supplied  
 88  # and draw their respective shapes if they accept the 
 89  # DrawingCanvas visior. 
 90  # 
91 -class DrawingCanvas:
92 93 ## 94 # Create the default object. 95 #
96 - def __init__(self):
97 self.width = 0; 98 self.height = 0; 99 self.image = None; 100 self.draw = None;
101 102 ## 103 # Create a new image to draw on with width, height and background colour (0,0,0,0) 104 # @param width See above. 105 # @param height See above.
106 - def createImage(self, width, height):
107 self.image = Image.new('RGBA', (width, height), (0, 0, 0, 0)); 108 self.width = width; 109 self.height = height;
110 111 ## 112 # Set the image to draw on as image which has width, height. 113 # @param image The image to draw on. 114 # @param width See above. 115 # @param height See above.
116 - def setImage(self, image, width, height):
117 self.image = image; 118 self.width = width; 119 self.height = height;
120 121 ## 122 # Visit all the elements in the element list and draw their shapes. 123 # @param elementList See above.
124 - def drawElements(self, elementList):
125 if(self.draw == None): 126 self.draw = ImageDraw.Draw(self.image); 127 for element in elementList: 128 element.acceptVisitor(self); 129 return self.image;
130 131 ## 132 # Get the fill colour from the ShapeSettings object from it's tuple. 133 # @param shapeSetting See above. 134 #
135 - def getFillColour(self, shapeSettings):
136 return shapeSettings[1][0];
137 138 ## 139 # Get the stroke colour from the ShapeSettings object from it's tuple. 140 # @param shapeSetting See above. 141 #
142 - def getStrokeColour(self, shapeSettings):
143 return shapeSettings[0][0];
144 145 ## 146 # Get the stroke width from the ShapeSettings object from it's tuple. 147 # @param shapeSetting See above. 148 #
149 - def getStrokeWidth(self, shapeSettings):
150 return shapeSettings[0][1];
151 152 ## 153 # Draw an ellipse at (cx, cy) with major and minor axis (rx,ry). 154 # @param cx See above. 155 # @param cy See above. 156 # @param rx See above. 157 # @param ry See above. 158 # @param shapeSettings The shapes display properties(colour,etc). 159 # @param affineTransform The affine transform that the shape has to undergo before drawing.
160 - def drawEllipse(self, cx, cy, rx, ry, shapeSettings, affineTransform = None):
161 x = cx-rx; 162 y = cy-ry; 163 w = x+rx*2; 164 h = y+ry*2; 165 fillColour = self.getFillColour(shapeSettings); 166 strokeColour = self.getStrokeColour(shapeSettings); 167 strokeWidth = self.getStrokeWidth(shapeSettings); 168 self.draw.ellipse((x,y,w,h), fill = fillColour, outline = strokeColour);
169 170 ## 171 # Draw a rectangle at (x, y) with width, height (width, height). 172 # @param x See above. 173 # @param y See above. 174 # @param width See above. 175 # @param height See above. 176 # @param shapeSettings The shapes display properties(colour,etc). 177 # @param affineTransform The affine transform that the shape has to undergo before drawing.
178 - def drawRectangle(self, x, y, w, h, shapeSettings, affineTransform = None):
179 fillColour = self.getFillColour(shapeSettings); 180 strokeColour = self.getStrokeColour(shapeSettings); 181 strokeWidth = self.getStrokeWidth(shapeSettings); 182 if(affineTransform==None): 183 self.draw.rectangle((x,y,w,h), fill = fillColour, outline = strokeColour); 184 else: 185 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 186 newDraw = ImageDraw.Draw(im); 187 newDraw.rectangle((x,y,w,h), fill = fillColour, outline = strokeColour); 188 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 189 self.image.paste(newImage);
190 191 192 ## 193 # Draw an polygon with the points in pointTupleList which are [(x1, y1), (x2, y2)...]. 194 # @param pointTupleList See above. 195 # @param shapeSettings The shapes display properties(colour,etc). 196 # @param affineTransform The affine transform that the shape has to undergo before drawing.
197 - def drawPolygon(self, pointTupleList, shapeSettings, affineTransform = None):
198 fillColour = self.getFillColour(shapeSettings); 199 strokeColour = self.getStrokeColour(shapeSettings); 200 strokeWidth = self.getStrokeWidth(shapeSettings); 201 if(affineTransform==None): 202 self.draw.polygon(pointTupleList, fill = fillColour, outline = strokeColour); 203 else: 204 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 205 newDraw = ImageDraw.Draw(im); 206 self.draw.polygon(pointTupleList, fill = fillColour, outline = strokeColour); 207 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 208 self.image.paste(newImage);
209 210 ## 211 # Draw a line from (x1, y1) to (x2,y2). 212 # @param x1 See above. 213 # @param y1 See above. 214 # @param x2 See above. 215 # @param y2 See above. 216 # @param shapeSettings The shapes display properties(colour,etc). 217 # @param affineTransform The affine transform that the shape has to undergo before drawing.
218 - def drawLine(self, x1, y1, x2, y2, shapeSettings, affineTransform = None):
219 fillColour = self.getFillColour(shapeSettings); 220 strokeColour = self.getStrokeColour(shapeSettings); 221 strokeWidth = self.getStrokeWidth(shapeSettings); 222 if(affineTransform==None): 223 self.draw.line([(x1, y1), (x2, y2)], fill = strokeColour, width = strokeWidth); 224 else: 225 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 226 newDraw = ImageDraw.Draw(im); 227 self.draw.line([(x1, y1), (x2, y2)], fill = strokeColour, width = strokeWidth); 228 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 229 self.image.paste(newImage);
230 231 ## 232 # Draw an polyline with the points in pointTupleList which are [(x1, y1), (x2, y2)...]. 233 # @param pointTupleList See above. 234 # @param shapeSettings The shapes display properties(colour,etc). 235 # @param affineTransform The affine transform that the shape has to undergo before drawing.
236 - def drawPolyline(self, pointTupleList, shapeSettings, affineTransform = None):
237 fillColour = self.getFillColour(shapeSettings); 238 strokeColour = self.getStrokeColour(shapeSettings); 239 strokeWidth = self.getStrokeWidth(shapeSettings); 240 if(affineTransform==None): 241 self.draw.line(pointTupleList, fill = fillColour, width = strokeWidth); 242 else: 243 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 244 newDraw = ImageDraw.Draw(im); 245 self.draw.line(pointTupleList, fill = strokeColour, width = strokeColour); 246 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 247 self.image.paste(newImage);
248 249 ## 250 # Draw a mask at (x, y) with (width, height). 251 # @param x See above. 252 # @param y See above. 253 # @param width See above. 254 # @param height See above. 255 # @param bytes The mask in bytes. 256 # @param shapeSettings The shapes display properties(colour,etc). 257 # @param affineTransform The affine transform that the shape has to undergo before drawing.
258 - def drawMask(self, x, y, width, height, bytes, shapeSettings, affineTransform = None):
259 fillColour = self.getFillColour(shapeSettings); 260 mask = Image.fromstring('1', (width, height), bytes); 261 if(affineTransform==None): 262 self.draw.bitmap(x, y, mask, fill = fillColour); 263 else: 264 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 265 newDraw = ImageDraw.Draw(im); 266 self.draw.bitmap(x, y, mask, fill = fillColour); 267 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 268 self.image.paste(newImage);
269 270 ## 271 # Draw text at (x, y) with major and minor axis (rx,ryr). 272 # @param x See above. 273 # @param y See above. 274 # @param text The text to draw. 275 # @param shapeSettings The shapes display properties(colour,etc). 276 # @param affineTransform The affine transform that the shape has to undergo before drawing.
277 - def drawText(self, x, y, text, shapeSettings, affineTransform = None):
278 textColour = self.getStrokeColour(shapeSettings); 279 if(affineTransform==None): 280 self.draw.text((x, y), text, fill = textColour); 281 else: 282 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 283 newDraw = ImageDraw.Draw(im); 284 self.draw.text((x, y), text, fill = textColour); 285 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 286 self.image.paste(newImage);
287